1. 程式人生 > >python模塊—OS

python模塊—OS

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模塊—OS