python学习--标准库之os 实例(2)

#!/usr/bin/env python3
"""检测一个文件的访问模式
"""

import os

print('Testing:', __file__) #显示输出的文件名
print('Exists:', os.access(__file__, os.F_OK)) #判断一个文件是否存在
print('Readable:', os.access(__file__, os.R_OK)) #判断这个文件是否可读
print('Writable:', os.access(__file__, os.W_OK)) #判断这个文件是否可写
print('Executable:', os.access(__file__, os.X_OK)) #判断这个文件是否可执行

#给出任意文件的绝对路径,判断这个文件的读写执行权限
'''
步骤:
1. 用户输入一个绝对路径
2. 判断这个绝对路径是指目录还是文件
3. 如果是文件,判断这个文件的属性
'''
if __name__ == '__main__':
    while True:
        pathname = input('
请输入一个目录名或文件名: ')
        if os.path.isdir(pathname):
            print("{}: 是一个目录".format(pathname))
        elif os.path.isfile(pathname):
            print("{}: 是一个文件".format(pathname))
            print("属性:可读	:{}	可写:	{}	可执行:{}".format(pathname,
                os.access(pathname,os.R_OK),
                os.access(pathname,os.W_OK),
                os.access(pathname,os.X_OK)))
        else:
            print("{}: 不是目录或文件类型!".format(pathname))
原文地址:https://www.cnblogs.com/hayden1106/p/7879238.html