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 ;-) )

Answers


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


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.


Need Your Help

Why doesn't javascript execute in .php file loaded with Ext.Ajax.Request?

ajax extjs

I want to load .php files via ajax which execute ExtJS script as they load, which in turn modifies the existing ExtJS objects already present in the DOM.

Error CASTing value to FLOAT in SQL Server 2005

sql-server-2005 casting floating-point fixed-point

I'm trying to get a stored procedure to work for a co-worker who is out sick (and thus can't be asked for guidance).

PHP/MySQL Injection example

php mysql sql sql-injection

This is a follow-up to this question: Is PHP's addslashes vulnerable to sql injection attack? (thanks to everyone that replied over there).