
学习时总结的一些常用方法>>>>
目录函数
os.getcwd() 返回当前工作目录
os.chdir() 改变工作目录
os.listdir(path="path") 列举指定目录中的文件名
os.mkdir(path) 创建单层目录,目录存在抛出异常
os.makedirs(path) 递归创建目录
os.remove(path) 删除文件
os.rmdir(path) 删除单层目录,目录不能为空,否则异常
os.removedirs(path) 递归删除目录,由子目录到父目录逐个删除
os.rename(oldname, newname) 文件重命名
os.system(command) 运行系统的shell命令
os.walk(top) 遍历top路径下的所有子目录,返回一个三元,(路径, [包含目录], [包含文件])的生成器
路径函数
os.path.abspath(__file__) 作用: 获取当前脚本的完整路径
os.path.basename(path) 去掉目录路径,单独返回文件名
os.path.dirname(path) 去掉文件名,单独返回目录路径
os.path.join(path1,[path2,...]) 把path1,path2各个部分组合成一个路径名
os.path.split(path) 分割文件名与路径,返回(f_path,f_name)元组。如果完全使用目录,它会将最后一个目录作为文件名分离,且不会判断文件或者目录是否存在
os.path.splittext(path) 分离文件名与扩展名,返回(f_name,f_extension)元祖
os.path.getsize(file) 返回指定文件的尺寸,单位字节
os.path.getatime(file) 返回指定文件最近的访问时间,返回值为浮点型秒数,可用time模块中的gmtime()或者localtime()函数转换
os.path.getctime(file) 返回指定文件的创建时间,返回值为浮点型秒数,可用time模块中的gmtime()或者localtime()函数转换
os.path.getmtime(file) 返回指定文件最新修改时间,可用time模块中的gmtime()或者localtime()函数转换
返回值布尔型
os.path.exists(path) 判断指定路径(目录或者文件)是否存在
os.path.isabs(path) 判断制定路径是否为绝对路径
os.path.isdir(path) 判断指定路径是否存在且是一个目录
os.path.islink(path) 判断指定路径是否存在且是一个符号链接
os.path.isfile(path) 判断指定路径是否存在且是一个文件
os.path.ismount(path) 判断指定路径是否存在且是一个挂载点
os.path.samefile(path1,path2) 判断path1和path2两个路径是否指向同一个文件