python常用模块——os模块

python编程时,经常和文件、目录打交道,这就离不开os模块,os模块包含普遍的操作系统功能,与具体的平台无关,列举一些常用的命令。

1.os.name:字符串指示你正在使用的平台。windows是“nt”,linux是“posix”。

#linux下
>>> os.name
'posix'
#window下
>>> os.name
'nt'

 

2.os.getcwd():当前所在的路径。

>>> os.getcwd()
'/tmp'

 

3.os.chdir("dirname"):切换到指定的路径。

>>> os.chdir('/etc/sysconfig')
>>> os.getcwd()
'/etc/sysconfig'

 

4.os.curdir:返回当前目录(‘.’),没发现有实际意义。

>>> os.curdir
'.'

 

5.os.pardir:返回当前目录的父目录的字符串名(“..”),也没有任何实际的意义。

>>> os.pardir
'..'

 

6.os.mkdir("dirname"):创建一个目录。

>>> os.mkdir('file1')

 

7.os.makedirs("dirname1/dirname2"):创建一个多层的目录。

>>> os.makedirs('dir1/file2')

 

8.os.rmdir("dirname"):删除一个目录,如果目录中有其它的目录或者文件将会报错,不能去删除文件。

>>> os.rmdir('dir2')

 

9.os.remove("filename"):删除一个文件。

>>> os.remove('dir1/test1')

 

10.os.removedirs():删除一个空目录。

>>> os.removedirs('dir1')

 

11.os.listdir('dirname'):列出指定的目录下的文件或者目录。

>>> os.listdir('/tmp')
['dir1', 'dir2']
>>> os.listdir('/tmp/dir1')
['file1', 'file2']

12.os.rename("oldname","newname"):修改文件名或目录名。

>>> os.rename('dir1','newdir1')   #修改目录名
>>> os.rename('newdir1/file1','newfile1')   #修改文件名

13.os.stat('path/name'):查看文件或目录的信息。

>>> os.stat('newdir1')  #目录信息
os.stat_result(st_mode=16877, st_ino=34242941, st_dev=2051, st_nlink=2, st_uid=0, st_gid=0, st_size=19, st_atime=1510732290, st_mtime=1510732357, st_ctime=1510732357)
>>> os.stat('newdir1/file2')   #文件信息
os.stat_result(st_mode=33188, st_ino=34242960, st_dev=2051, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1510729923, st_mtime=1510729923, st_ctime=1510729923)

 

stat文件信息的参数
st_mode 保护模式
st_ino 节点号
st_dev 设备
st_nlink inode的链接数
st_uid 所有者的uid,所有者是root
st_gid 所有者的gid
st_size 文件的大小
st_atime 最后的访问时间
st_mtime  
st_ctime 最后的修改时间

 

 

 

 

 

 

 

 

 

 

 

14.os.sep:输出操作系统特定的路径分割符。

#linux下
>>> os.sep
'/'
#windows下
>>> os.sep
'\\'

 

15.os.linesep:输出当前平台使用的行终止符。

#Linux下
>>> os.linesep
'\n'
#windows下
>>> os.linesep
'\r\n'

 

16.os.pathsep:输出用于分割文件路径的字符串。

#linux下
>>> os.pathsep
':'
#window下
>>> os.pathsep
';'

 

17.os.system('bash command'):运行命令,显示结果。直接显示

>>> os.system('ifconfig')
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.130  netmask 255.255.255.0  broadcast 192.168.43.255
        ether 00:0c:29:f9:99:68  txqueuelen 1000  (Ethernet)
        RX packets 15245  bytes 1192629 (1.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6882  bytes 781568 (763.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 28  bytes 2380 (2.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 2380 (2.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

0

 

18.os.popen('bash command'):运行命令,获取执行的结果。返回一个可迭代对象

>>> os.popen('ifconfig')
<os._wrap_close object at 0x7fdbe4dcf1d0>   #可迭代对象
>>> list(os.popen('ifconfig'))
['ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n', '        inet 192.168.43.130  netmask 255.255.255.0  broadcast 192.168.43.255\n', '        ether 00:0c:29:f9:99:68 

 

19.os.environ:获取系统的环境变量

>>> os.environ
environ({'XDG_SESSION_ID': '10', 'HOSTNAME': '7.2-2.cx.com', 'SELINUX_ROLE_REQUESTED': '', 'TERM': 'xterm', 'SHELL': '/bin/bash',

 

20.os.path.abspath(path):返回绝对路径

>>> os.path.abspath('dir2')
'/tmp/dir2'

 21.os.path.exists(path):判断path是否存在,不存在返回False,存在返回True。

>>> os.path.exists('/tmp')
True
>>> os.path.exists('/tmp/test2')
False

 

22.os.path.isabs(path):判断path是否是绝对路径。

>>> os.path.isabs('dir2')
False
>>> os.path.isabs('/tmp/test2')
True

 

23.os.path.isfile(path):判断path是否是一个文件。

>>> os.path.isfile('/tmp/newdir1/file2')
True
>>> os.path.isfile('/tmp/newdir1')
False

 

24.os.path.isdir(path):判断path是否是一个目录。

>>> os.path.isdir('/tmp/newdir1')
True
>>> os.path.isdir('/tmp/newdir1/file2')
False

 

25.os.path.join(path1,path2....):可以将多个路径组合在一起。

>>> os.path.join('/tmp/newdir1','file2')
'/tmp/newdir1/file2'
>>> os.path.join('/tmp/newdir1','/tmp/file2')
'/tmp/file2'      #路径中不能有重叠的地方
>>> os.path.join('/tmp/newdir1','file2/file3/file4')
'/tmp/newdir1/file2/file3/file4'   #路径可能不存在

 

26.os.path.getatime(path):返回path文件或目录最后的访问时间。

>>> os.path.getatime('/etc/passwd')
1510725661.0741642

#可以转化一下
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getatime('/etc/passwd')))
'2017-11-15 14:01:01'

 

 

27.os.path.getmtime(path):返回path文件或目录最后的修改时间

>>> os.path.getctime('/script/color.py')
1510228018.6768353

#转化一下
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getctime('/script/color.py')))
'2017-11-09 19:46:58'

 

28.os.path.getsize(path):返回path的大小

>>> os.path.getsize('/etc/passwd')
1045

 

posted @ 2017-11-15 16:58  明王不动心  阅读(895)  评论(0编辑  收藏  举报