os模块常用的文件处理函数
函数 使用说明
access(path, mode) 按照mode指定的权限访问文件
open(path, flags, mode=0o777, *, dir_fd=None) 按照mode指定的权限打开文件,默认权限为可读、可写、可执行
chmod(path, mode, *, dir_fd=None, follow_symlinks=True) 改变文件的访问权限
remove(path) 删除指定的文件
rename(src, dst) 重命名文件或目录
stat(path) 返回文件的所有属性
fstat(path) 返回打开的文件的所有属性
listdir(path) 返回path目录下的文件和目录列表
startfile(filepath [, operation]) 使用关联的应用程序打开指定文件
os.path模块常用的文件处理函数
函数名称 使用说明
abspath(path) 返回绝对路径
dirname(p) 返回目录的路径
exists(path) 判断文件是否存在
getatime(filename) 返回文件的最后访问时间
getctime(filename) 返回文件的创建时间
getmtile(filename) 返回文件的最后修改时间
getsize(filename) 返回文件的大小
isabs(path)、isdir(path)、isfile(path) 判断path是否为绝对路径、目录、文件
split(path) 对路径进行分割,以列表形式返回
splitext(path) 从路径中分割文件的扩展名
splitdrive(path) 从路径中分割驱动器的名称
walk(top,func,arg) 遍历目录
os模块常用的目录操作函数
函数名称 使用说明
mkdir(path[,mode=0777]) 创建目录
makedirs(path1/path2…,mode=511) 创建多级目录
rmdir(path) 删除目录
removedirs(path1/path2…) 删除多级目录
listdir(path) 返回指定目录下所有文件信息
getcwd() 返回当前工作目录
chdir(path) 把path设为当前工作目录
walk(top,topdown=True,onrror=None) 遍历目录树
seq 当前操作系统所使用的路径分隔符
extsep 当前操作系统所使用的文件扩展名分隔符