码迷,mamicode.com
首页 > 编程语言 > 详细

python os

时间:2014-09-24 14:26:17      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   使用   文件   div   sp   

python os模块——来自互联网:

 

Python os模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要.

常用方法

1、os.name

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

2、os.getcwd()

函数得到当前工作目录,即当前Python脚本工作的目录路径。

3、os.listdir()

返回指定目录下的所有文件和目录名。

>>> os.listdir(os.getcwd())
[‘Django‘, ‘DLLs‘, ‘Doc‘, ‘include‘, ‘Lib‘, ‘libs‘, ‘LICENSE.txt‘, ‘MySQL-python-wininst.log‘, ‘NEWS.txt‘, ‘PIL-wininst.log‘, ‘python.exe‘, ‘pythonw.exe‘, ‘README.txt‘, ‘RemoveMySQL-python.exe‘, ‘RemovePIL.exe‘, ‘Removesetuptools.exe‘, ‘Scripts‘, ‘setuptools-wininst.log‘, ‘tcl‘, ‘Tools‘, ‘w9xpopen.exe‘]
>>> 

4、os.remove()

删除一个文件。

5、os.system()

运行shell命令(有些shell命令用不了)。

>>> os.system(‘dir‘)
0
>>> os.system(‘cmd‘) #启动dos

6、os.sep 

操作系统特定的路径分割符。

7、os.linesep

当前系统的换行符

>>> os.linesep
‘\r\n‘            #Windows使用‘\r\n‘,Linux使用‘\n‘而Mac使用‘\r‘。
>>> os.sep
‘\\‘              #Windows
>>> 

8、os.path.split()

函数返回一个路径的目录名和文件名

>>> os.path.split(‘C:\\Python25\\abc.txt‘)
(‘C:\\Python25‘, ‘abc.txt‘)

9、os.path.isfile()和os.path.isdir()

分别检验给出的路径是一个文件还是目录。

>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile(‘a.txt‘)
False

10、os.path.exists()

函数用来检验给出的路径是否真地存在

>>> os.path.exists(‘C:\\Python25\\abc.txt‘)
False
>>> os.path.exists(‘C:\\Python25‘)
True
>>> 

11、os.path.abspath(fileName)

获得绝对路径 

>>> os.path.abspath("2.txt")
‘/srv/infra/mongodb/2.txt‘
>>> 

12、os.path.getsize(name)

获得文件大小,如果name是目录返回0L 

>>> os.path.getsize("2.txt")
597

13、os.path.dirname(path)

返回文件路径

>>> os.path.dirname(‘c:\\Python\\a.txt‘)
‘c:\\Python‘

14、os.path.splitext()

分离文件名与扩展名

>>> os.path.splitext(‘a.txt‘)
(‘a‘, ‘.txt‘)

15、os.path.join(path,name)

连接目录与文件名或目录

>>> os.path.join(‘c:\\Python‘,‘a.txt‘)
‘c:\\Python\\a.txt‘
>>> os.path.join(‘c:\\Python‘,‘f1‘)
‘c:\\Python\\f1‘
>>> 

16、os.path.basename(path)

返回文件名

>>> os.path.basename(‘a.txt‘)
‘a.txt‘
>>> os.path.basename(‘c:\\Python\\a.txt‘)
‘a.txt‘
>>> 

python os

标签:des   style   blog   color   os   使用   文件   div   sp   

原文地址:http://www.cnblogs.com/muzhongjiang/p/3990436.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!