File and IO 文件和IO 读写文本数据 1 2 with open ("text.txt" , "rt" , encoding="utf-8" ) as f: pass
打印输出到文件中 1 2 with open ("text.log" , "wt" ) as f: print ("Test log" , file=f)
使用其它分隔符或打印终止符打印 1 2 3 4 5 print ('abcd' , 1234 , sep=',' , end='\t' )row = ('abcd' , 1234 , 'bcda' , 3 ) print (',' .join(str (x) for x in row))print (*row, sep="," )
读写字节数据 1 2 with open ("text.bin" , "rb" ) as f: pass
文件不存在时才写入 本操作可以避免覆盖已经写入的文件。
1 2 with open ("text.txt" , "xt" ) as f: pass
字符串的IO操作 可以使用 io.StringIO() 和 io.BytesIO() 类来创建类文件对象操作字符串数据。比如:
1 2 3 4 5 6 7 8 9 10 s = io.StringIO() s.write('Hello World' ) print ('This is a test' , file=s)print (s.getvalue())s = io.StringIO('Hello\nWorld\n' ) print (s.read(4 ))print (s.read())
StringIO 只能用于文本,如果需要对二进制文件进行操作,需要使用 io.BytesIO() 代替。
读写压缩文件 使用 gzip 或者 bz2 模块进行操作,基本也是使用 *.open() 方法进行读写操作。
固定大小记录的文件迭代 1 2 3 4 5 6 7 from functools import partialRECORD_SIZE = 32 with open ('file.dat' , 'rb' ) as f: records = iter (paertial(f.read, RECORD_SIZE), b'' ) pass
文件路径名的操作 使用 os.path 模块来进行文件名称和路径的操作。
1 2 3 4 5 6 7 8 9 10 import osimport timeos.path.exists('/etc/passwd' ) os.path.isdir('/etc' ) os.path.islink('/usr/bin/python' ) os.path.realpath('/usr/bin/python' ) os.path.getsize('/etc/passwd' ) time.ctime(os.path.getmtime('/etc/passwd' )) os.listdir('/etc' )