views:

92

answers:

3

I need to check if I can write a file to a directory that user point to with Python.

Is there an way to check it in advance? I may be using try .. catch for this purpose, but I expect something better in the sense that I can check in advance.

A: 

The pythonic way is to access it and catch the exception if it fails.

If you really have to check it, use os.access, but the results are not always true, beware of issues in Vista/Win7 with UAC, for example!

Example:

os.access(r'C:\Programme', os.R_OK)

This will tell you if you have read access.

leoluk
+2  A: 

You will probably not find something better or more Pythonic. Python's philosophy is it is easier to ask forgiveness than permission.

You can use os.access if you like. Coupled with os.path.isfile to check if you have a file and not e.g. a directory. It will probably give you what you need. The exception path is much better.

Muhammad Alkarouri
-1 for reiterating Python philosophy with no discussion of why exception handling is the better solution here.
Joe
@Joe: I linked to the Python philosophy. Also, it is a philosophy because it works here and elsewhere in Python. It is useful for duck typing for example.
Muhammad Alkarouri
@Muhammad: The Python glossary doesn't explain why it's useful though; in fact it describes it as "clean and fast", and I disagree strongly with both of those.
Joe
@Joe: I agree that it is not fast. It is clean in my opinion. More importantly, EAFP is coming at the problem from a different angle to you. Your answer is "because of the race condition" which is correct but is invariant to the programming language. My answer is "use EAFP" because it is useful for all sorts of things _in Python_, not just because of the race condition. The OP might find both answers useful in different ways.
Muhammad Alkarouri
+3  A: 

Despite Jim Brissom's claim, exception handling is not cheap in Python compared to 'check then try' idioms if you expect the thing to fail more than a few percent of the time. (Read to the end for an exception!) However, the key thing here is that you need to check the exception anyway, because the permissions can change between the check and your write:

### !!! This is an example of what not to do!
### !!! Don't do this!
if os.access("test", os.W_OK):
    # And in here, some jerk does chmod 000 test
    open("test", "w").write(my_data)
    # Exception happens despite os.access!

os.access and the stat module are great if you're trying to, e.g., prepare a list of folders for the user to pick and want to exclude invalid ones a priori. However, when the rubber hits the road, it is not a replacement for exception handling if you want your program to be robust.

And now the exception to the exceptions-are-slow rule: Exceptions are slow, but disks are slower. And if your disk is under particularly heavy load, you might end up having the file you're interested in evicted from the OS or disk cache between the os.access and open call. In this case, you're going to experience a major slowdown as you have to go to disk (and these days, that can also mean network) twice.

Joe
I am not going to downvote you on a technicality, but you might want to update your answer for a _write_, not a read. Use `os.W_OK` and `open("test", "w")`.
Muhammad Alkarouri
Thanks, I've fixed it, although the example was to show the race condition, not the answer. In fact it's an example of what _not_ to do.
Joe
I'm afraid I have to ask you for another change. I know the example isn't the most important part of your answer, but opening a file for writing then _reading_ from it is not a pretty sight.
Muhammad Alkarouri