File and Directory Access
os.path
windows的路径要写成:
C:\\test\\sub\\
import os
function:
os.path.expanduser(path) # 把path中~或~user扩展成绝对路径 expanduser("~/src") -> /home/user/src
os.path.expandvars(path) # 把path中的shell变量$var 或 ${var} 还原.
os.path.dirname(filename) # 返回filename的路径 dirname("/home/user/file.py") -> /home/user
os.path.join(a, *p) # 拼结一个完整的路径
os.path.join(a, os.pardir) # 返回上级目录的路径
os.path.realpath(filename) # 返回filename的真实路径+文件名 realpath('__file__')
os.path.abspath(path) # 返回绝对路径, os.path.abspath('__file__')
os.path.splitext(p) # 分解路径和扩展名返回组成的元组,/home/user/test.py -> ("/home/user/test", ".py")
os.path.basename(p) # 返回最后一个组件名,也就是文件名 /home/user/test.py -> test.py
os.path.getsize(filename) # 返回文件大小
os.path.exists(path) # 判断path(文件或目录)是否存在
os.path.isfile(path) # 判断path是否是常规文件
pathlib
new in python3.4
from pathlib import Path
from pathlib import PurePosixPath
from pathlib import PureWindowsPath
stat
fileinput
filecmp
fnmatch
linecache
glob
functions:
glob(pathname) # 返回匹配pathname路径下正则表达式的所有文件组成的列表
iglob(pathname) # 同上,返回generator.
shutil
Utility functions for copying and archiving files and directory trees.
import shutil
functions:
copy(src, dst)
copy2(src, dst)
...
rmtree(path, ignore_errors=False, onerror=None) # 删除指定的目录,path不能是文件.
unregister_archive_format(name)
move(src, dst)
...
tempfile
macpath
ConfigParser
一般用来处理*.ini文件,格式为:
[section]
option-key = option-value
导入:
import ConfigParser
classes:
ConfigParser.ConfigParser(defaults=None)
# methods:
read(filenames) # 读取ini文件
sections() # 获取所有section
options(section) # 获取section的所有option
get(section, option, raw=False, vars=None) # 返回字符串
getint(section, options)
getfloat(section, options)
getboolean(section, options) # 大小写都可以:0/1, false/true, no/yes, off/on
set(section, option, value)
has_section(section)
has_option(section, option)
remove_section(section)
remove_option(section, option)
write(fp)
issue:
默认是全部小写写入.
config = ConfigParser.ConfigParser(allow_no_value=True)
config.optionxform =str # 原样写入
from configparser import ConfigParser, ExtendedInterpolation
变量:
// 默认interpolation只支持section之间变量
[section1]
1var1 = /opt
1var2 = %(1var1)s/test
// 扩展interpolation支持section变量引用
config = ConfigParser(interpolation=ExtendedInterpolation())
[section1]
1var1 = /opt
[section2]
2var1 = ${section1:1var1}/test
File Formats
csv
import csv
classes:
# csv.DictReader
DictReader(self, f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
# methods
next()
# csv.DictWriter
DictWriter(self, f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwargs)
# mthods
writeheader()
writerow(rowdict)
writerows(rowdicts)
functions:
// 返回DictReader对象
reader(iterable, dialect='excel', **kwargs)
// 返回DictWriter对象
writer(fileobj, dialect='excel', **kwargs)
data: