[Tutor] Problem with os.access function. [semantic error, if check does not work]

Alan Gauld alan.gauld at freenet.co.uk
Sun Dec 25 17:06:17 CET 2005


Take a look at my draft OS topic(click the url below).

It will point you at several other functions that will be of use...

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld/tutos.htm


----- Original Message ----- 
From: "Panagiotis Atmatzidis" <p.atmatzidis at gmail.com>
To: "Python Tutor" <Tutor at python.org>
Sent: Saturday, December 24, 2005 1:20 PM
Subject: [Tutor] Problem with os.access function. [semantic error,if check 
does not work]


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
      check_path = os.access(path, mode)
      print check_path
      if check_path == 'False':
         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':
                  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':
            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




More information about the Tutor mailing list