python中的os模块 操作系统接口

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() 是否为文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值