您的位置:首页 > 编程语言 > Python开发

Python os模块 使用示例

2016-08-25 20:18 549 查看
Python os模块包含了普遍的操作系统功能,与具体的平台无关。

os模块常用方法如下:

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

#windows下:
>>> import os
>>> os.name
'nt'
#Linux下:
>>> import os
>>> os.name
'posix'
os.getcwd()  得到当前工作目录,即当前Python脚本工作的目录路径。

os.listdir() 返回指定目录下的所有文件和文件夹。

>>> os.getcwd()
'C:\\Users\\91135\\Desktop\\test'
>>> os.listdir(os.getcwd())
['1.txt', '2.txt', '3.txt', 'test1']
>>> os.listdir('.')
['1.txt', '2.txt', '3.txt', 'test1']
>>> os.listdir('./')
['1.txt', '2.txt', '3.txt', 'test1']

os.remove() 删除指定的文件。

os.rmdir()删除指定的空目录。。。

>>> os.listdir('./')
['1.txt', '2.txt', '3.txt', 'test1','test2', 'test3']
>>> os.remove('1.txt')
>>> os.listdir('.')
['2.txt', '3.txt', 'test1','test2', 'test3']
>>> os.rmdir('test1')
>>> os.listdir('.')
['2.txt', '3.txt', 'test2', 'test3']
os.mkdir()创建目录  注意:这样只能建立一层,要想递归建立可用:os.makedirs()

os.rename()重命名文件。。。

>>> os.listdir('.')
['2.txt', '3.txt', 'test2', 'test3']
>>> os.mkdir('test1')
>>> os.makedirs('test4\\test5')
>>> os.listdir('.')
['2.txt', '3.txt', 'test1','test2', 'test3','test4']
>>> os.rename('3.txt','1.txt')
>>> os.listdir('.')
['1.txt', '2.txt', 'test1','test2', 'test3','test4']
os.system() 运行shell命令。

#windows下:
>>> os.system('cmd') #启动dos
#Linux下:
>>> os.system('dir')
Desktop    Downloads	     features  Pictures  Public     test.py
Documents  examples.desktop  Music     plugins	 Templates  Videos
0
>>> os.system('ls')
Desktop    Downloads	     features  Pictures  Public     test.py
Documents  examples.desktop  Music     plugins	 Templates  Videos
0
#注意:运行shell命令时,如果要调用python之前的变量,可以用如下方式:
#Linux下:
>>> import os
>>> x=825
>>> os.environ['x']=str(x)#注意此处[]内的是字符串,等号右边也是字符串
>>> os.system('echo $x')
825
0
os.chdir() 改变当前目录,到指定目录中。

>>> os.getcwd()
'C:\\Users\\91135\\Desktop\\test'
>>> os.chdir('C:\\Users\\91135\\Desktop')
>>> os.getcwd()
'C:\\Users\\91135\\Desktop'
os.sep 变量主要用于系统路径中的分隔符(取代操作系统特定的路径分割符)。。。

Windows系统通过是“\\”,Linux类系统如Ubuntu的分隔符是“/”,而苹果Mac OS系统中是“:”。

Python 是跨平台的,在 Windows 上,文件的路径分割符号是 '\' ,在 Linux 上 是 ‘/’。

为了让代码在不同的平台上都能运行,那么写路径的时候是写 ‘/’ 还是写 '\' 呢?

使用 os.sep 的话,就不用去考虑这个了,os.sep 根据所处的平台,自动地采用相应的分割符号。

举例:

Linux下一个路径,  /usr/share/python,那么上面的 os.sep 就是 ‘/’

Windows下一个路径, C:\Users\Public\Desktop, 那么上面的 os.sep 就是 '\'。

#windows下:
>>> os.sep
'\\'
#linux下:
>>> os.sep
'/'
os.linesep 给出当前平台使用的行终止符,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'

#windows下
>>> os.linesep
'\r\n'
#linux下
>>> os.linesep
'\n'
os.path.split() 返回一个路径的目录名和文件名,此处只是把前后两部分分开而已,就是找最后一个'\'

>>> os.path.split(r'C:\Users\91135\Desktop\test\1.txt')
('C:\\Users\\91135\\Desktop\\test', '1.txt')
>>> os.path.split(r'C:\Users\91135\Desktop\test')
('C:\\Users\\91135\\Desktop', 'test')
>>> os.path.split('C:\\Users\\91135\\Desktop\\test\\')
('C:\\Users\\91135\\Desktop\\test', '')
>>> os.path.split('C:/Users/91135/Desktop/test/')
('C:/Users/91135/Desktop/test', '')
os.path.isdir()判断指定对象是否为目录。是True,否则False

>>> os.path.isdir(os.getcwd())
True
>>> os.path.isdir('.')
True
>>> os.listdir('.')
['1.txt', '2.txt', 'test2', 'test3']
>>> os.path.isdir('test2')
True
>>> os.path.isdir('2.txt')
False
os.path.isfile()判断指定对象是否为文件。是返回True,否则False

>>> os.listdir('./')
['1.txt', '2.txt', 'test2', 'test3']
>>> os.path.isfile('1.txt')
True
>>> os.path.isfile('test2')
False
>>> os.path.isfile('333.txt')#该文件不存在,返回False
False
os.path.exists()判断给出的路径(或文件)是否真地存在

>>> os.path.exists(os.getcwd())
True
>>> os.listdir('./')
['1.txt', '2.txt', 'test1', 'test2']
>>> os.path.exists('test1')
True
>>> os.path.exists('./1.txt')
True
os.path.abspath(name)或os.path.realpath(name) 获得绝对路径

>>> os.listdir('.')
['1.txt', '2.txt', 'test2', 'test3']
>>> os.path.abspath('.')
'C:\\Users\\91135\\Desktop\\test'
>>> os.path.realpath('./')
'C:\\Users\\91135\\Desktop\\test'
>>> os.path.abspath('test2')
'C:\\Users\\91135\\Desktop\\test\\test2'
>>> os.path.realpath('1.txt')
'C:\\Users\\91135\\Desktop\\test\\1.txt'
os.path.normpath(path):规范path字符串形式

>>> os.listdir('.')
['1.txt', '2.txt', 'test2', 'test3']
>>> os.path.normpath('2.txt')
'2.txt'
>>> os.path.normpath(r'c:\users\911358\desktop')
'c:\\users\\911358\\desktop'
>>> os.path.normpath('c:/users/911358/desktop')
'c:\\users\\911358\\desktop'
os.path.getsize(name):获得文件大小,如果name是目录返回0L

os.path.splitext():分离文件名与扩展名 【extension:延伸,扩展】

>>> os.path.getsize('C:\\Users\\91135\\Desktop\\test')
0L
>>> os.path.getsize('C:\\Users\\91135\\Desktop\\test\\1.txt')
37L
>>> os.path.splitext('test.txt')
('test', '.txt')
>>> os.path.splitext('C:\\Users\\91135\\Desktop\\test.txt')
('C:\\Users\\91135\\Desktop\\test', '.txt')
>>> os.path.splitext(r'C:\Users\91135\Desktop\1.txt')
('C:\\Users\\91135\\Desktop\\1', '.txt')
os.path.join(path,name):连接目录与文件名(或目录)

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

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

>>> os.path.join('C:\\Users\\91135\\Desktop\\test','1.txt')
'C:\\Users\\91135\\Desktop\\test\\1.txt'
>>> os.path.join('C:\\Users\\91135\\Desktop','test')
'C:\\Users\\91135\\Desktop\\test'
>>> os.path.basename('1.txt')
'1.txt'
>>> os.path.basename('C:\\Users\\91135\\Desktop\\test\\1.txt')
'1.txt'
>>> os.path.dirname('C:\\Users\\91135\\Desktop\\test\\1.txt')
'C:\\Users\\91135\\Desktop\\test'
小例子=》 test.py文件:

#!/usr/bin/env python
#coding:utf-8
import os,sys
if __name__ == "__main__":
print sys.argv
print os.path.realpath(sys.argv[0])
print os.path.abspath(sys.argv[0])
print os.path.split(os.path.realpath(sys.argv[0]))
print os.path.split(os.path.realpath(sys.argv[0]))[0]
运行结果:
song@ubuntu:~$ python test.py arg1 arg2

['test.py', 'arg1', 'arg2']

/home/song/test.py

/home/song/test.py

('/home/song', 'test.py')

/home/song

song@ubuntu:~$

关于其他os模块的介绍,请参考:

1》python os.system()和os.popen()

2》python os.system(command)函数的返回值 与 linux命令返回值的关系

3》python 中的split()函数和os.path.split()函数

4》python的 os 和 shutil 模块
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: