【Python】Python读取文件夹下的所有文件
你水喝多了:
具体操作参考了下:https://zhuanlan.zhihu.com/p/265694493和博主
感谢二位,代码贴这,用者自取
访问所有文件代码:
[code=python]
def check_file(path):
if not os.path.exists(path):
print("File not exist")
return False
if(os.path.isdir(path)):
# 操作文件夹
if not os.access(path, os.R_OK):
print("File is accessible to read")
return False
# todo
print(os.path.basename(path))
for file in os.listdir(path):
file = os.path.join(path,file)
check_file(file)
else:
# 操作文件
if not os.access(path, os.R_OK):
print("File is accessible to read")
return False
with open(path) as f:
# todo
print(f.name)
[/code]
|