import os
# os.F_OK: 检查文件是否存在;
# os.R_OK: 检查文件是否可读;
# os.W_OK: 检查文件是否可以写入;
# os.X_OK: 检查文件是否可以执行
if os.access("/file/path/foo.txt", os.F_OK):
print "Given file path is exist."
if os.access("/file/path/foo.txt", os.R_OK):
print "File is accessible to read"
if os.access("/file/path/foo.txt", os.W_OK):
print "File is accessible to write"
if os.access("/file/path/foo.txt", os.X_OK):
print "File is accessible to execute"
遍历文件夹下的所有文件和目录
# -*- coding:utf-8 -*-
import os
for root, dirs, files in os.walk(r"E:\test"):
# 获取所有的文件
for file in files:
# 获取文件所属目录
print(root)
# 获取文件的名称
print(file)
# 获取文件的路径
print(os.path.join(root, file))
# 获取所有的目录
for dir in dirs:
# 获取目录的名称
print(dir)
# 获取目录的路径
print(os.path.join(root, dir))
# 参考:https://blog.csdn.net/sinat_29957455/article/details/82778306