python 文件读写文件
Before reading or writing a file, access should be checked first. How to detect whether a file is readable and writable in Python?
在读取或写入文件之前,应先检查访问权限。 如何检测文件在Python中是否可读写?
You can use the
您可以使用
os.access(path, mode)
library function https://docs.python.org/release/2.6.6/library/os.html#os.access like the Linux access
library function for C.
库函数https://docs.python.org/release/2.6.6/library/os.html#os.access,例如C语言的Linux access
库函数。
It returns True if access is allowed, False if not.
如果允许访问,则返回True,否则返回False。
For readable and writable, you can test file path
with mode
set to:
对于可读性和可写性,您可以在以下mode
测试文件path
:
os.R_OK
操作系统R_OK
Value to include in the mode parameter of access() to test the readability of path.
os.W_OK
操作系统W_OK
Value to include in the mode parameter of access() to test the writability of path.
翻译自: https://www.systutorials.com/how-to-detect-whether-a-file-is-readable-and-writable-in-python/
python 文件读写文件