确定目录是否可写 [英] Determining Whether a Directory is Writeable
本文介绍了确定目录是否可写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中确定目录是否可以为执行脚本的用户写入的最佳方式是什么?因为这可能涉及使用os模块,我应该提到我在* nix环境下运行它。
What would be the best way in Python to determine whether a directory is writeable for the user executing the script? Since this will likely involve using the os module I should mention I'm running it under a *nix environment.
推荐答案
虽然Christophe建议的是一个更加Pythonic的解决方案,但是os模块确实有 os.access函数来检查访问:
Although what Christophe suggested is a more Pythonic solution, the os module does have the os.access function to check access:
os.access('/ path / to / folder',os.W_OK)
#W_OK用于写入,R_OK用于阅读等。
os.access('/path/to/folder', os.W_OK)
# W_OK is for writing, R_OK for reading, etc.
这篇关于确定目录是否可写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文