开发者

Check if I can write a file to a directory or not with Python

开发者 https://www.devze.com 2023-01-16 01:28 出处:网络
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

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.


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.


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.


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.


just use this:

def check_access(file, mode):
   try:
      open(file, mode)
      return True
   except PermissionError:
      return False
print(check_access("test", "w"))

output if no Permission:

False

output if Permission:

True

if you use os.access() then you have a chance of getting something like

   Traceback (most recent call last):
 File "/Users/lawrence/Library/Application Support/CodeRunner/Unsaved/Untitled.py", line 8, in <module>
   open("test", "w").write("test")
 PermissionError: [Errno 13] Permission denied: 'test'

you can still use os.access() but I don't recommend it

0

精彩评论

暂无评论...
验证码 换一张
取 消