python OS模块归纳

上一篇 / 下一篇  2016-03-23 16:41:09 / 个人分类:python

os模块中包含普遍的操作系统功能

1.os.name:输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'。

2.os.getcwd():函数得到当前工作目录,即当前Python脚本工作的目录路径。

3.os.listdir():返回指定目录下的所有文件和目录名。
os.listdir("E:\\")
['$RECYCLE.BIN', '1.txt', '11.txt', '111.bat', '111.txt', '测试case,素材集合']

4.os.remove():删除一个文件。
os.remove('E:\\m.txt')
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    os.remove('E:\\m.txt')
WindowsError: [Error 32] 另一个程序正在使用此文件,进程无法访问。: 'E:\\m.txt'

5.s.system():运行shell命令。
os.system('cmd') #启动dos

6.os.sep 表示当前操作系统的路径分割符。
>>> os.sep
'\\'

7.os.linesep字符串给出当前平台使用的行终止符

8.os.path.dirname(path):返回文件路径
>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'

9.os.path.basename(path):返回文件名
 os.path.basename('c:\\Python\\a.txt')
'a.txt'

10.os.path.join(path,name):连接目录与文件名或目录
os.path.join('c:\\Python','a.txt')
'c:\\Python\\a.txt'

11.os.path.exists()函数用来检验给出的路径是否真地存在

12.s.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。

13.os.chdir()  把当前系统运行目录切换到给定的文件目录
>>> f='C:\\'
>>> os.chdir(f)
>>> os.getcwd()
'C:\\'

14.os.listdir() 需遍历的文件遍历出来放列表中
l=os.listdir(os.curdir)
>>> l
['$360Section', '$Recycle.Bin', '$Windows.~BT', '23bbefc28a7ec909fffdd7a8', '2b268db5017a23b4940dfc24', '360Downloads', 

'360Rec', '360SANDBOX', '360安全浏览器下载', '4692dc72cec55f4d70a86ef57bbb4f', '5bdf33b13c95f65481de6d55', 'Alimama', 

'apache-ant-1.9.4-bin', 'Config.Msi', 'cygwin64', 'dell', 'Documents and Settings', 'DOS', 'DTLDownLoads', 'DTLFolder', 

'ea062e308065caf3807d36', 'hiberfil.sys', 'Intel', 'MSOCache', 'p', 'pagefile.sys', 'PerfLogs', 'Program Files', 

'Program Files (x86)', 'ProgramData', 'Python27', 'Python33', 'Recovery', 'System Volume Information', 

'UniAccessAgentDownloadData', 'Users', 'vcredist_x86.log', 'Vista', 'webapp', 'Win 7', 'Windows', 'XP', '_log', '抓LOG工

具']

15.os.curdir:表示当前的目录环境

16.os.pardir:当前目录的上一层目录

TAG: Python python

 

评分:0

我来说两句

Open Toolbar