Jase
Jase

Reputation: 517

Python 2.7, OS portable way to determine permissions / contents of a puser entered path

Disclaimer: i have searched genericly (Google) and here for some information on this topic, but most of the answers are either very old, or don't seem to quite make sense to me, so I appologize in advance if this seems simple or uninformed.

Problem: My app accepts command line input which may be either a path or a file and I need to determine several things about it.

  1. Is it a path or file,
  2. Is it relative or absolute
  3. Is it readable and / or writable (need read and write test results)(ignoring the possibility of a race situation)

One caveat, while a

    try:
        file=open(filename,'w')
    except OSError as e:
        {miscellaneous error handling code here}

would obviously tell me if the parameter (filename in above example) existed/ was writable etc. I do not understand enough about exception codes to know how to interpret the result of exception. It also wouldn't provide the relative/absolute information.

Assuming that there is no one method to do this then I would need to know three things:

  1. How to determine relative / absolute
  2. Is it pointing to a file or a directory
  3. can the EUID of the program read the location, and same for write.

I am seeking to learn from the information I gather here, and I am new to python and have bit off a significant project. I have mastered all of it except this part. Any help would be appreciated. (Pointers to good help sites welcome!)(except docs.python.org that ones bookmarked already ;-) )

Upvotes: 2

Views: 250

Answers (2)

Himanshu Jindal
Himanshu Jindal

Reputation: 637

Here are your answers. The documentations specifies that the following works for both windows and linux.

  1. How to determine relative / absolute os.path.isabs returns True if the path is absolute, False if not.

  2. Is it pointing to a file or a directory Similarly Use os.path.isdir to find out if the path is directory or not. YOu can use os.path.isfile(path) to find out if the path is file or not.

  3. can the EUID of the program read the location, and same for write. You can use os.access(path, mode) to know if operations requiring permissions specified by mode are possible on file specified by path or not. P.S. This will not work for files being accessed over the network. You can use os.stat This is the right way to get all the information. However it is a more costly call and hence, you should try and get all the info in one call . To interpret the results, you can use the stat module

Upvotes: 1

ChrisC
ChrisC

Reputation: 1272

Seems like os.path would take care of most of these needs with isabs(), isfile(), isdir(), etc. Off the top of my head I can't think of a function that will give a simple True/False for read/write access for a given file, but one way to tackle this might be to use the os module's stat function and have a look at the file's permissions and owner.

Upvotes: 1

Related Questions