status |
草稿 |
清风 & liz; 100% |
Contents
PCS200 os(.stat;.path)
概述
os.path是一个与平台无关的文件路径处理模块.它可以帮助我们解决在程序处理中碰到的一些复杂的路径处理问题,如"//,"\","\\","."等路径分隔符处理不全面的问题\跨平台问题和组合一个可用的跨平台的路径地址的问题.这些路径处理问题我们用简单的字符串拆分是很困难或者无法完成的,但是用os.path都替你做好了的解决方案,你只用只须使用它里面的函式就可以了.
使用
os.path.split
函式功能: os拆分路径,返回一个tuple,第一个元素是文件所在路径,第二个元素是对应文件名.如下一个小测试:
1 import os.path
2
3 for path in [ '/one/two/three',
4 '/one/two/three/',
5 '/',
6 '.',
7 '']:
8 print '"%s" : "%s"' % (path, os.path.split(path))
对于一个文件(具有绝对路径或者相对路径),使用os.path.split()可以将其拆成对应路径名(不以路径分割符,如'/'结尾)和文件名;对于一个路径,则返回该路径(同样,不以路径分割符,如'/'结尾),而对应的文件名为空.这里比较奇特的是对于当前文件夹('.'),os.path.split()把它看作是文件名.
~$ python pcs-200-1.py
"/one/two/three" : "('/one/two', 'three')"
"/one/two/three/" : "('/one/two/three', '')"
"/" : "('/', '')"
"." : "('', '.')"
"" : "('', '')"
os.path.basename
函式功能: 只获取某路径对应的文件名.修改上述的例子:
1 import os.path
2
3 for path in [ '/one/two/three',
4 '/one/two/three/',
5 '/',
6 '.',
7 '']:
8 print '"%s" : "%s"' % (path, os.path.basename(path))
如果这里的函式参数只是一个路径,不是文件名,则返回对应的文件名为空.同样对于'.'也是比较奇怪.
~$ python pcs-200-2.py "/one/two/three" : "three" "/one/two/three/" : "" "/" : "" "." : "." "" : ""
os.path.dirname
函式功能: 只获取某路径对应的路径,不含文件名,再修改之前的例子:
1 import os.path
2
3 for path in [ '/one/two/three',
4 '/one/two/three/',
5 '/',
6 '.',
7 '']:
8 print '"%s" : "%s"' % (path, os.path.dirname(path))
同样,返回的路径名是不以路径分割符,如'/'结尾的.
~$ python pcs-200-3.py "/one/two/three" : "/one/two" "/one/two/three/" : "/one/two/three" "/" : "/" "." : "" "" : ""
os.path.splitext
函式功能: 将路径,文件名,扩展名分开,并以一个tuple的形式返回:
1 import os.path
2
3 for path in [ 'filename.txt', 'filename', '/path/to/filename.txt', '/', '' ]:
4 print '"%s" :' % path, os.path.splitext(path)
可以看到os.path.splittext只是很单纯的将文件名和扩展名分开了.
~$ python pcs-200-4.py
"filename.txt" : ('filename', '.txt')
"filename" : ('filename', '')
"/path/to/filename.txt" : ('/path/to/filename', '.txt')
"/" : ('/', '')
"" : ('', '')
os.path.commonprefix
函式功能: 看一个比较cool的功能就是要在一组路径中,找到一个共同的前缀,比如:
1 import os.path
2
3 paths = ['/one/two/three/four',
4 '/one/two/threefold',
5 '/one/two/three/',
6 ]
7 print paths
8 print os.path.commonprefix(paths)
~$ python pcs-200-5.py ['/one/two/three/four', '/one/two/threefold', '/one/two/three/'] /one/two/three
os.path.join
函式功能: 使用os.path.join组合一些零散的字符串,生成一个安全的路径表示.
1 import os.path
2
3 for parts in [ ('one', 'two', 'three'),
4 ('/', 'one', 'two', 'three'),
5 ('/one', '/two', '/three'),
6 ]:
7 print parts, ':', os.path.join(*parts)
也就是将一系列短路径拼合成有效的长路径.
~$ python pcs-200-6.py
('one', 'two', 'three') : one/two/three
('/', 'one', 'two', 'three') : /one/two/three
('/one', '/two', '/three') : /three
os.path.expanduser
可以利用os.path.expanduser寻找用户的home目录.
1 import os.path
2
3 for user in [ '', 'root', 'mysql' ]:
4 lookup = '~' + user
5 print lookup, ':', os.path.expanduser(lookup)
'~user'是表示某个用户的home目录,'~'表示当前用户的home目录。
~$ python pcs-200-7.py ~ : /home/shengyan ~root : /root ~mysql : /var/lib/mysql
os.path.expandvars
函式功能: 使用os.path.expandvars来读取路径中系统环境变量的值.下面示例中,先使用os.environ增加定义了一个环境变量'MYVAR',并赋其值为'VALUE',然后使用os.path.expandvars将出现在路径中的环境变量扩展为对应值.
1 import os.path
2 import os
3
4 os.environ['MYVAR'] = 'VALUE'
5
6 print os.path.expandvars('/path/to/$MYVAR')
~$ python pcs-200-8.py /path/to/VALUE
os.path.normpath
函式功能: 处理不规则路径字符串,转化为正常的路径.比如:
1 import os.path
2
3 for path in [ 'one//two//three',
4 'one/./two/./three',
5 'one/../one/two/three',
6 ]:
7 print path, ':', os.path.normpath(path)
~$ python pcs-200-9.py one//two//three : one/two/three one/./two/./three : one/two/three one/../one/two/three : one/two/three
os.path.abspath
函式功能: 将相对路径转换为绝对路径.
1 import os.path
2
3 for path in [ '.', '..', './one/two/three', '../one/two/three']:
4 print '"%s" : "%s"' % (path, os.path.abspath(path))
~$ python pcs-200-10.py "." : "/home/shengyan/LovelyPython/PCS/pcs-200" ".." : "/home/shengyan/LovelyPython/PCS" "./one/two/three" : "/home/shengyan/LovelyPython/PCS/pcs-200/one/two/three" "../one/two/three" : "/home/shengyan/LovelyPython/PCS/one/two/three"
问题
要注意,在两个根目录做join操作时经常出现的问题:
1 import os
2 print os.path.join('/tmp','/var')
执行结果:
'/var'
从结果中可以看到,并不是将两个路径做简单连接,通过看help("os.path.join")可以看到,路径的拼接是从/开始的,所以会以后一个开始/为初始路径,要特别注意.
探讨
如果不使用os.path这个模块,自己手工处理以上的例子,可以试试看需要如何处理,会遇到哪些困难.
-- 清风 2008-04-25 14:33:00