Python文件對象
明確文件系統:
- 獲取文件對象:
var_name = open(file_name[mode,[bufsize]])
緩沖:
0:禁用
1:使用緩沖,只緩沖一行數據
2+:指定緩存空間大小
負數:使用系統默認緩沖區
- 文件對象的內置方法、函數、屬相
next:
In [10]: f1 = open('/etc/passwd','r') In [11]: type(f1) Out[11]: file In [12]: f1.next() Out[12]: 'root:x:0:0:root:/root:/bin/bash\n' In [13]: f1.next() Out[13]: 'bin:x:1:1:bin:/bin:/sbin/nologin\n' In [14]: f1.next() Out[14]: 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n'
close:
In [35]: f1.close()
fileno 返回文件描述符
In [38]: f1 = open('/etc/passwd','r') In [39]: f1.fileno() Out[39]: 8
readline,readlines
返回字符串對象
In [40]: f1.readline() Out[40]: 'root:x:0:0:root:/root:/bin/bash\n' 返回文件所有行為列表對象: In [41]: f1.readlines() Out[41]: ['bin:x:1:1:bin:/bin:/sbin/nologin\n', 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n', 'mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n', 'nobody:x:99:99:Nobody:/:/sbin/nologin\n']
tell:游標在文件中的位置,字節
In [47]: f1.tell() Out[47]: 951
file.seek(offset[whence])
whence:起點
0:從文件頭偏移,默認
1:從當前位置偏移
2:從文件尾部偏移
In [48]: f1.tell() Out[48]: 951 In [49]: f1.seek(0) In [50]: f1.tell() Out[50]: 0 file.read([size])
file.read([size]) 讀取多少個字節
In [51]: f1.read(10) Out[51]: 'root:x:0:0' 此時在readline,或next讀取的是位置到行尾 In [52]: f1.readline() Out[52]: ':root:/root:/bin/bash\n'
file.write打開文件保存數據
In [61]: cp /etc/passwd . In [62]: ls passwd In [64]: f1 = open('passwd','r+') In [65]: f1.next() Out[65]: 'root:x:0:0:root:/root:/bin/bash\n' I [66]: f1.seek(0,2) In [67]: f1.tell() Out[67]: 951 In [68]: f1.write('new line.\n') In [69]: f1.tell() Out[69]: 961 In [70]: cat passwd root:x:0:0:root:/root:/bin/bash mysql:x:306:306::/home/mysql:/bin/bash new line. In [71]: f1.close() In [72]: f2 = open('new_file','w+') In [73]: f2.write('Python') In [74]: f2.close() In [75]: cat new_file Python 讀模式打開不存在文件,報IOError In [76]: f3 = open('new_file2','r+') --------------------------------------------------------------------------- IOError Traceback (most recent call last) <ipython-input-76-1d0d8a99b4f2> in <module>() ----> 1 f3 = open('new_file2','r+') IOError: [Errno 2] No such file or directory: 'new_file2' In [77]: f3 = open('new_file2','a') In [78]: ls new_file new_file2 passwd
writelines(...)
writelines(sequence_of_strings) -> None. Write the strings to the file.
Note that newlines are not added. The sequence can be any iterable object
producing strings. This is equivalent to calling write() for each string
In [13]: f4 = open('new_file4','w+') In [14]: import os In [15]: l4 = os.listdir(‘/etc’)
返回列表對象:
In [19]: f4.writelines(l4)
In [20]: f4.flush()
可以看到,writelines把列表中多有對象當成一個字符串寫入文件,沒有換行
下面進行手動換行:
重新生成列表,添加換行符:
In [23]: l4 = [i+'\n' for i in os.listdir('/etc')]
In [25]: f4 = open('new_file4','w+') In [26]: f4.writelines(l4) In [27]: f4.flush() In [28]: f4.close()
isatty()判斷是不是終端
In [52]: f3 = open('new_file3','r+') In [53]: f3.isatty() Out[53]: False
truncate(N)截取保留N個字節:
In [55]: f3.readline() Out[55]: 'root:x:0:0:root:/root:/bin/bash\n' In [56]: f3.readline() Out[56]: 'bin:x:1:1:bin:/bin:/sbin/nologin\n' In [57]: f3.readline() Out[57]: 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n' 截取保留當前游標位置及之前字節數 In [58]: f3.truncate(f3.tell()) In [59]: f3.flush() In [60]: f3.seek(0) In [61]: f3.readlines() Out[61]: ['root:x:0:0:root:/root:/bin/bash\n', 'bin:x:1:1:bin:/bin:/sbin/nologin\n', 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n']
closed 屬性,判斷文件打開狀態
In [62]: f4.closed Out[62]: False
file.name屬性
In [53]: f1.name Out[53]: '/etc/passwd'
mode文件打開模式,另外還有encoding、softspace等。
file.close file.flush file.next file.seek file.writelines file.closed file.isatty file.read file.softspace file.xreadlines file.encoding file.mode file.readinto file.tell file.errors file.name file.readline file.truncate file.fileno file.newlines file.readlines file.write
- 練習
1、1-10的平方寫進文件new_file3,一次寫一個對象
2、上面writelines方法,寫入/etc下所有文件名到文件。
In [7]: f3 = open('new_file3','w+') In [8]: for line in (i**2 for i in range(1,11)): ...: f3.write(str(line)+'\n') ...: In [9]: f3.flush() In [10]: f3.close() In [11]: cat new_file3 1 4 9 16 25 36 49 64 81 100