TypeError while checking for permissions with os.access() on windows xp

Simon Forman sajmikins at gmail.com
Fri Aug 21 20:20:53 CEST 2009

On Aug 21, 1:33 pm, ryniek90 <rynie... at gmail.com> wrote:
> I've got some code that checks priviliges on two paths:
> First - chosen by user
> Second - hardcoded home directory represented by **os.getenv('HOME')** -
> (os.getenv('HOME') works both on Linux and Windows)
> Here's the code:
> "
> def __check_set_perm(self, rd_obj_path, backup_dest):
>         try:
>             if os.path.exists(rd_obj_path):
>                 if os.access(rd_obj_path, os.R_OK) != True:
>                     print "Have no permissions on [%s] for reading
> operation.\nTrying to set them..." % os.path.split(rd_obj_path)[1]
>                     if not os.path.isdir(rd_obj_path):
>                         os.chmod(rd_obj_path, stat.S_IREAD)
>                     else:
>                         for root, dirs, files in os.walk(rd_obj_path):
>                             for f in files:
>                                 os.chmod(os.path.join(root, f),
> stat.S_IREAD)
>                     print "Get permissions for reading on [%s]
> successfully." % os.path.split(rd_obj_path)[1]
>                 else:
>                     print "Have permissions on [%s] for reading." %
> os.path.split(rd_obj_path)[1]
>                 if os.access(backup_dest, os.W_OK) != True:
>                     print "Have no permissions on [%s] for writing
> operation.\nTrying to set them..." % os.path.split(backup_dest)[1]
>                     os.chmod(backup_dest, stat.S_IWRITE)
>                     print "Get permissions for reading on [%s]
> successfully." % os.path.split(backup_dest)[1]
>                 else:
>                     print "Have permissions on [%s] for writing." %
> os.path.split(backup_dest)[1]
>             else:
>                 return "Can't find specified path - [%s]." % rd_obj_path
>                 sys.exit(0)
>         except (Except), ex:
>             return ex
> "
> This code is part of my backup script. When script checks for reading
> permission on 'user chosen path' it seems ok, but when it checks for
> writing permissions on 'home directory', (this line: **if
> os.access(backup_dest, os.W_OK) != True**), i get error:
> **TypeError: coercing to Unicode: need string or buffer, NoneType found**
> The second os.access check fails, but don't know why. When i turned
> os.W_OK to integer, i get the same error.
> But when changed os.W_OK or that int to string, i get error that integer
> is required. I'm launching this code on WinXP Prof+SP3, on Administrator
> account. Is it bug in os.acces() or my code is written wrong?
> Btw. i kow that os.chmod in windows can set only read-olny flag, so
> later i'll change the line with **os.chmod(path, stat.S_IWRITE)**
> Thanks and cheers.

First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write "if some_boolean_expression != True:"
instead prefer "if not some_boolean_expression:".  Also, in your code
"except (Except), ex: return ex" the parentheses are redundant,
there's no "Except" Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that "sys.exit(0)" doing in there? And why exit with 0 if there
was a problem? Exit code 0 means "no problem".)

Second, from what you've said backup_dest must be None:

>>> import os
>>> os.access(None, os.W_OK)

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

More information about the Python-list mailing list