python 文件读写文件_如何检测文件在Python中是否可读写?

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.
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-detect-whether-a-file-is-readable-and-writable-in-python/

python 文件读写文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值