[Tutor] Problem with os.access function. [semantic error, if check does not work]
Panagiotis Atmatzidis
p.atmatzidis at gmail.com
Mon Dec 26 12:34:35 CET 2005
Hello,
Thank you both for the tip's and the interesting links. I resolved my
problem easily using another function, os.path.isdir(x) which was more
specific.
Best Regards.
On 12/24/05, bob <bgailer at alum.rpi.edu> wrote:
> At 05:20 AM 12/24/2005, Panagiotis Atmatzidis wrote:
> >Hello,
> >
> >I am writing a function in order to check if a directory exists. If
> >exists the functions must do nothing, otherwise must check the users
> >permissions and if it's possible create the dir. Looking at pydoc's
> >httpd I found the module "os" and the function "access". From the
> >http-doc:
> >
> >access(...)
> >access(path, mode) -> 1 if granted, 0 otherwise
> >
> >Use the real uid/gid to test for access to a path. Note that most
> >operations will use the effective uid/gid, therefore this routine can
> >be used in a suid/sgid environment to test if the invoking user has the
> >specified access to the path. The mode argument can be F_OK to test
> >existence, or the inclusive-OR of R_OK, W_OK, and X_OK.
> >
> >This is my function:
> >
> >def homedirhandle():
> > path = "/some/dir/" # check the existance of the
> >directory
> > mode = 755
>
> should be mode = 0755 (octal representation) for mkdir. For
> access: "The mode argument can be F_OK to test existence, or the
> inclusive-OR of R_OK, W_OK, and X_OK." suggests that only 1 digit is expected.
>
> > check_path = os.access(path, mode)
> > print check_path
> > if check_path == 'False':
>
> Should be if check_path == False:
>
> Or even simpler if not check_path:
>
> Use print repr(check_path). Then you'd see either True or 'True'.
> That would help you see whether check_path is boolean or string.
>
> > print ""
> > print "Directory /some/dir does not exist."
> > print "Trying to create the directory."
> > uid = os.geteuid()
> > print "the uid is ", uid
> > if uid == '0':
>
> I think (not having UNIX access at the moment) that this should be if uid == 0:
>
> > try:
> > os.mkdir(path, mode)
> > print ""
> > print "The directory has been created."
> > print ""
> > return path
> > except OSError, e:
> > print ""
> > print >>sys.stderr, "The mkdir command failed:
> >%d (%s)" % (e.errno, e.strerror)
> > print ""
> > print "Exiting"
> > sys.exit(1)
> >
> > if check_path == '1':
>
> == 1
>
> > print ""
> > print "The directory /some/dir has been created."
> > print ""
> > return path
> > else:
> > print "Please create the directory /some/dir manually and
> >then re-run vuhalndler."
> > print "Exiting"
> > sys.exit()
> > else:
> > print ""
> > print "The directory already exists."
> > print ""
> > return path
> >
> >Now the problem lies at the first check " if check_path == 'False':
> >". It's a semantic error, the program does not really check the dir,
> >it just takes for granted that the dir exists. I tried with 1 before
> >putting "False" there.. but it did not work so I took the print result
> >of check_path and substitute 1 with False. But still nothing :-(
> >
> >Why does not make the check? I thought that the functions
> >functionality was clear.. probably is not.
> >
> >
> >
> >--
> >Panagiotis
> >_______________________________________________
> >Tutor maillist - Tutor at python.org
> >http://mail.python.org/mailman/listinfo/tutor
>
>
--
Panagiotis
More information about the Tutor
mailing list