python 常用模块os系统接口

python 常用模块os系统接口

os模块是提供对操作系统进行调用的接口

http://www.cnblogs.com/alex3714/articles/5161349.html

 

os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: ('.')
os.pardir  获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多层递归目录
os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录
os.stat('path/filename')  获取文件/目录信息
os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  运行shell命令,直接显示
os.popen() 方法用于从一个命令打开一个管道。
os.environ  获取系统环境变量
os.path.abspath(path)  返回path规范化的绝对路径
os.path.split(path)  将path分割成目录和文件名二元组返回
os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path)  返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  如果path是绝对路径,返回True
os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间

 

>>> os.getcwd()
'/home/admin/wangxu'
>>> os.curdir  
'.'
>>> os.pardir  
'..'
>>> os.makedirs('test/test1')
>>> os.removedirs('test/test1')
>>> os.mkdir('aaaa')
>>> os.rmdir('aaaa')
>>> os.listdir('.')
[]
>>> os.listdir('.') 
['ccc']
>>> os.remove
<built-in function remove>
>>> os.listdir('.')
['ccc']
>>> os.remove()    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'path' (pos 1) not found
>>> os.listdir('.')
['ccc']
>>> os.remove(ccc) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ccc' is not defined
>>> os.remove('ccc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IsADirectoryError: [Errno 21] Is a directory: 'ccc'
>>> os.remove()     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'path' (pos 1) not found
>>> os.listdir('.') 
['111', 'ccc']
>>> os.remove('ccc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IsADirectoryError: [Errno 21] Is a directory: 'ccc'
>>> os.remove('111')
>>> os.listdir('.') 
['ccc']
>>> os.rename('ccc','ddd')
>>> os.listdir('.')       
['ddd']
>>> os.stat('ddd')
os.stat_result(st_mode=16893, st_ino=666556, st_dev=51713, st_nlink=2, st_uid=503, st_gid=503, st_size=4096, st_atime=1527487627, st_mtime=1527487627, st_ctime=1527487755)
>>> os.sep
'/'
>>> os.linesep
'\n'
>>> os.pathsep
':'
>>> os.name
'posix'
>>>
>>> os.system()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'command' (pos 1) not found
>>> os.system('ls')
ddd
0
>>> a=os.system('ls')  
111  bbb  ddd  nwdir
>>> a
0
>>> a
0
>>> b=os.popen('ls')
>>> b.read()
'111\nbbb\nddd\nnwdir\n'
>>> b.read()        
''
>>> b=os.popen('ls')
>>> print(b.read()) 
111
bbb
ddd
nwdir

>>>



>>> os.system('mkdir aaa')
0
>>> os.system('ls')       
aaa  ddd
0
>>> os.environ
environ({'HOSTNAME': 'pe-jira', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'HISTSIZE': '1000', 'SSH_CLIENT': '113.139.191
>>> os.path.abspath('/home/admin/wangxu')
'/home/admin/wangxu'
>>> os.path.abspath('.')
'/home/admin/wangxu'
>>> os.path.abspath('..')
'/home/admin'
>>>
>>> os.path.split('/usr/local/nagios')
('/usr/local', 'nagios')
>>> os.path.split('/usr/local')       
('/usr', 'local')
>>> os.path.dirname('.')
''
>>> os.path.dirname('/home/admin')
'/home'
>>> os.path.basename('/home/admin/wangxu')
'wangxu'
>>> os.path.exists('/tmp')
True
>>> os.path.exists('/tmpS')
False
>>> os.path.basename('/home/admin')
'admin'
>>> os.path.isabs('/home/admin')
True
>>> os.path.isabs('../admin')   
False
>>> os.path.isdir('../wangxu')
True
>>> os.path.isdir('../wangxu21')
False
>>> os.list
os.listdir(    os.listxattr(  
>>> os.listdir('.')
['aaa', '111', 'ddd']
>>> os.path.isdir('./111')      
False
>>> os.path.isdir('./aaa')
True
>>>
>>> os.path.isfile('111')
True
>>> os.path.isfile('aaa')
False
>>>
>>> os.path.join('/home')       
'/home'
>>> os.path.join('/home','/usr')
'/usr'
>>> os.path.join('/home','/usr','../')
'/usr/../'
>>> os.path.join('/home','/usr','../','../../')
'/usr/../../../'
>>> os.path.getmtime('bbb')
1527487818.840968
>>> os.path.getctime('bbb')
1527490155.7976348
>>> os.stat('bbb')
os.stat_result(st_mode=16893, st_ino=668009, st_dev=51713, st_nlink=2, st_uid=503, st_gid=503, st_size=4096, st_atime=1527487818, st_mtime=1527487818, st_ctime=1527490155)
>>>

 

posted on 2018-05-28 15:11  光阴8023  阅读(401)  评论(0编辑  收藏  举报