python OS模块归纳
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(mostrecentcalllast):
File"",line1,in
os.remove('E:\\m.txt')
WindowsError:[Error32]另一个程序正在使用此文件,进程无法访问。:'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','DocumentsandSettings','DOS','DTLDownLoads','DTLFolder',
'ea062e308065caf3807d36','hiberfil.sys','Intel','MSOCache','p','pagefile.sys','PerfLogs','ProgramFiles',
'ProgramFiles(x86)','ProgramData','Python27','Python33','Recovery','SystemVolumeInformation',
'UniAccessAgentDownloadData','Users','vcredist_x86.log','Vista','webapp','Win7','Windows','XP','_log','抓LOG工具']
15.os.curdir:表示当前的目录环境
16.os.pardir:当前目录的上一层目录
以上内容为大家介绍了pythonOS模块归纳,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。http://www.mobiletrain.org/

相关推荐HOT
更多>>
python怎么输入多变量
Python同时输入多个值 1.同时输入多个字符串,字符串间以逗号/空...详情>>
2023-11-12 23:43:43
python岗位主要做什么工作?
python的工作岗位还是很多的,涉及的工作方向也很广,下面举一些常见的岗位,大家可以参考:Web应用开发Python经常被用于Web开发。例如,通过mo...详情>>
2023-11-12 23:05:27
python能做网站吗?
python可以做网站,Python有很多优秀的网站框架,可以非常快速的建一个网站。比如django之类的框架。Django、TurboGears、Eurasia、UliWeb等:...详情>>
2023-11-12 21:47:07
python中的int是什么意思?
python中的int()函数用于将一个字符串或数字转换为整型。语法以下是int()方法的语法:classint(x,base=10)参数x--字符串或数字。base--进制数,...详情>>
2023-11-12 21:21:30