os 模块

__file__获取当前模块所在路径

 

一、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命令。

>>> 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(file_path)

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

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

9.os.path.splitext(file_path)

  直接得到文件扩展名

>>> s = 'http://www.baidu.com/image/1.jpg'
>>> os.path.splitext(s)
('http://www.baidu.com/image/1', '.jpg')

 

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

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

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

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

12、os.path.abspath(name):获得绝对路径

>>>os.path.abspath(__file__)
D:\web\test3.py

 

13、os.path.normpath(path):规范path字符串形式

14、os.stat(filename).st_size  :获得文件大小,获得的值类型为int, 单位bytes

>>>size = os.stat("D:\\web\\test3.py").st_size
>>>print(size, type(size))
4205 <class 'int'>

 

15、os.path.getsize(filename):获得文件大小,内部是由  os.stat(filename).st_size 实现的

 

16、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'
>>> 

17、os.path.basename(path):返回文件名

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

18、os.path.dirname(path):返回文件路径

>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'
>>> os.path.dirname(__file__)
'D:\web'

 19、os.path.join() 和 os.path.dirname()可以嵌套使用

直接使用os.path.dirname(__file__)有时得到相对路径,使用os.path.dirname(os.path.abspath(__file__)) 无论何时都获得绝对路径。

例:

>>>print(os.path.join(os.path.dirname(os.path.abspath(__file__)), "test3.py"))
'D:\web\test3.py'

 20、os.walk(dir_path)

返回一个对象;

os.walk()可以得到一个三元tupple(root_dir, sub_dirs, files),其中第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件。
其中root_dir是一个string,代表目录的路径,sub_dirs是一个list,包含了root_dir下所有子目录的名字。files是一个list,包含了非目录文件的名字。这些名字不包含路径信息,如果需要得到全路径,需要使用os.path.join(root_dir, name).

dirList = os.walk(os.path.abspath("."))
    for root_dir,sub_dir, files in dirList:
        for file in files:
            print(file)

结果:
log.log
Logging.py
python 线程.py
利用logging记录登录成功失败信息.py
装饰器.py
记录错误.py
misc.xml
modules.xml
workspace.xml
xxxxxx.iml

 

posted @ 2016-05-19 11:36  Echo/  阅读(418)  评论(0编辑  收藏  举报