python库的学习系列之 10. File and Directory Access
时间:2011-01-22 来源:白天的小萤火虫
10.1. os.path — Common pathname manipulations
os.path.abspath(path) 找到path(以当前位置为准的相对位置)代表的文件的绝对路径。
>>> print os.path.abspath('t')
C:\Python26\t
>>> print os.path.normpath(os.getcwd() + '\\t')
C:\Python26\t
os.path.basename(path)
提供系统中路径操作相关的函数,很常用的、文件路径、路径归一化、判断路径是文件夹还是文件,以及获取文件大小,文件更改,创建时间等等很多接口,有一个walk函数与os.walk差不多使用。
10.2. fileinput — Iterate over lines from multiple input streams
一次处理多个文件,让代码少点,看起来更加优雅。
10.3. stat — Interpreting stat() results
文件,文件夹等等的属性判断。Stat()
10.4. statvfs — Constants used with os.statvfs()
与10.3差不多的作用。
10.5. filecmp — File and Directory Comparisons
文件与文件夹之间的比较。
10.6. tempfile — Generate temporary files and directories
临时文件和文件夹,在进程结束后会自动回收。
10.7. glob — Unix style pathname pattern expansion
在某一目录下找与之匹配的文件,作用好比os.listdir() and fnmatch.fnmatch()联合使用的效果。
前者遍历dir下的文件,后者按照格式过滤出符合的文件。
10.8. fnmatch — Unix filename pattern matching
文件名的过滤模式。
10.9. linecache — Random access to text lines
从任何一个文件中读取指定行到cache中。
10.10. shutil — High-level file operations
这个是处理文件的高级用法,实现了shell中的cp,mv,rm等,以及cp -r等操作,还有打包tar,gz等等的函数make_archive等等。
10.11. dircache — Cached directory listings
这个和10.9的linecache一样,只不过这是针对dircache。
相关阅读 更多 +