标签:python
OS模块也是我们平时工作中很常用到的一个模块,通过os模块调用系统命令,获得路劲,获取操作系统的类型等都是使用该模块。
1.获取系统类型
例子:
>>> import os
>>> print(os.name)
posix
2.执行系统命令
例子1:
>>> import os
>>> os.system("ifconfig")
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.230 netmask 255.255.254.0 broadcast 192.168.3.255
inet6 fe80::b8ce:3bee:4221:ea32 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:13:a3:a6 txqueuelen 1000 (Ethernet)
RX packets 6804684 bytes 830957408 (792.4 MiB)
RX errors 0 dropped 222021 overruns 0 frame 0
TX packets 111583 bytes 16210846 (15.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
例子2:
popen() 返回一个file对象,通过 file.read()获取最终结果
>>> content = os.popen("ifconfig").read()
>>> print(content)
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.230 netmask 255.255.254.0 broadcast 192.168.3.255
inet6 fe80::b8ce:3bee:4221:ea32 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:13:a3:a6 txqueuelen 1000 (Ethernet)
RX packets 6813500 bytes 832023527 (793.4 MiB)
RX errors 0 dropped 222508 overruns 0 frame 0
TX packets 111846 bytes 16233128 (15.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.文件和目录操作
3.1 当前路径:os.getcwd()
例子:
>>> print(os.getcwd())
/root
3.2 切换目录:os.chdir(‘目标目录’)
例子:
>>> os.chdir("/data/temp") # 或者 os.chdir(r"/root")
>>> os.getcwd()
‘/data/temp‘
3.3 列出文件:os.listdir(‘字符串目录’)
例子:
>>> os.listdir(os.getcwd())
[‘.bash_logout‘, ‘.bash_profile‘, ‘.cshrc‘, ‘.tcshrc‘, ‘anaconda-ks.cfg‘, ‘.bash_history‘, ‘.bashrc‘, ‘4.sh‘, ‘10.sh‘, ‘.jenkins‘, ‘.groovy‘, ‘.python-eggs‘, ‘.mongorc.js‘, ‘.dbshell‘, ‘.gitconfig‘, ‘.java‘, ‘.viminfo‘, ‘test.sh‘, ‘.mysql_history‘, ‘.pki‘, ‘tip.sh‘, ‘.oracle_jre_usage‘, ‘.rnd‘, ‘.bashrc_2017-08-21‘, ‘2.sh‘, ‘.cache‘, ‘.config‘, ‘6.txt‘, ‘centos_hzp.tar‘, ‘container.tar‘, ‘.ssh‘, ‘.bashrc_2017-08-18‘, ‘1.sh‘, ‘5.sh‘, ‘.npm‘]
>>> os.listdir("/data/temp")
[‘health_8102.log‘, ‘health_1-3.log‘, ‘projects1.txt‘, ‘create_branch_status‘, ‘test‘, ‘create_branch.sh‘, ‘api.txt.bak‘, ‘projects.txt.bak‘, ‘modify_configure‘, ‘test2‘]
3.4 创建目录:os.mkdir(‘目录‘)
例子
>>> os.mkdir("test20171023")
>>> os.listdir(os.getcwd())
[‘health_8102.log‘, ‘health_1-3.log‘, ‘projects1.txt‘, ‘create_branch_status‘, ‘test‘, ‘create_branch.sh‘, ‘api.txt.bak‘, ‘projects.txt.bak‘, ‘modify_configure‘, ‘test2‘, ‘test20171023‘]
3.5 删除目录下文件os.remove(‘文件名‘)
例子
>>> os.remove("1.txt")
说明:不存在则报错
3.6 打印系统分隔符:os.linesep
例子
print(os.linesep)
说明:linux为\n ;windows为\r\n ;mac为\r
3.7 显示文件目录:os.path.dirname(r‘/data/temp/1.txt‘)
例子
>>> print(os.path.dirname(r‘/data/temp/1.txt‘))
/data/temp
3.8 拼接多级目录:os.path.join(os.getcwd(), ‘aaa‘, ‘bbb’, ‘ccc’)
例子
>>> print(os.path.join(os.getcwd(), ‘abc.txt‘))
/data/temp/abc.txt
说明:并不创建
3.9 文件和路径分开:os.path.split(path1)
例子
>>> path1 = os.path.join(os.getcwd(), ‘abc.txt‘)
>>> print(os.path.split(path1))
(‘/data/temp‘, ‘abc.txt‘)
4.0 路径和扩展名分开:os.path.splitext(‘文件’)
例子
>>> path1 = os.path.join(os.getcwd(), ‘abc.txt‘)
>>> print(os.path.splitext(path1))
(‘/data/temp/abc‘, ‘.txt‘)
4.1 文件不存在则创建:os.path.exists(‘目录’)
例子
#! /usr/bin/python
import os
if not os.path.exists(r‘/data/temp/test20171024‘):
os.mkdir(r‘/data/temp/test20171024‘)
4.2 是否链接:os.path.islink(path)
例子
>>> print(os.path.islink(os.getcwd()))
False
说明:如果系统不支持链接,则返回False
更多方法:
print(dir(os))
..............
标签:python
原文地址:http://huangzp.blog.51cto.com/12434999/1980340