python os模块 其他操作系统接口
python官宣内容
This module provides a portable way of using operating system dependent functionality.
If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.
翻译:该模块提供了一种使用操作系统相关功能的便携方式。
如果您只想读取或写入文件,请参阅open(),如果要操作路径,请参阅os.path模块,如果要读取命令行中所有文件中的所有行,请参阅fileinput模块。 有关创建临时文件和目录的信息,请参阅tempfile模块,有关高级文件和目录的处理,请参阅shutil模块。
简单说就是:os可以利用它提供的方法和当前的操作系统交互
os模块常用方法
os.remove(‘path/filename’) 删除文件
os.rename(oldname, newname) 重命名文件
os.walk() 生成目录树下的所有文件名
os.chdir(‘dirname’) 改变目录
os.mkdir/makedirs(‘dirname’)创建目录/多层目录
os.rmdir/removedirs(‘dirname’) 删除目录/多层目录
os.listdir(‘dirname’) 列出指定目录的文件
os.getcwd() 取得当前工作目录
os.chmod() 改变目录权限
os.path.basename(‘path/filename’) 去掉目录路径,返回文件名
os.path.dirname(‘path/filename’) 去掉文件名,返回目录路径
os.path.join(path1[,path2[,…]]) 将分离的各部分组合成一个路径名
os.path.split(‘path’) 返回( dirname(), basename())元组
os.path.splitext() 返回 (filename, extension) 元组
os.path.getatime\ctime\mtime 分别返回最近访问、创建、修改时间
os.path.getsize() 返回文件大小
os.path.exists() 是否存在
os.path.isabs() 是否为绝对路径
os.path.isdir() 是否为目录
os.path.isfile() 是否为文件