1. 程式人生 > >(二)2-5 文件操作

(二)2-5 文件操作

常用 追加 串操作 常用方法 orm als spa size 標準

文件操作
Python提供了標準輸入和輸出進行讀寫。
a、讀取文件內容
當前目錄下有個text.txt,文件內容如下:
11111
22222
33333
44444
55555
編寫text.py ,內容如下:
readlines()方法:讀取文件內容,文件內容的每一行都是一個字符串,最後返回一個字符串。
import codecs
f = codecs.open(‘text.txt‘)
print(f.readlines())
f.close()
運行結果:
[‘11111\r\n‘, ‘22222\r\n‘, ‘33333\r\n‘, ‘44444\r\n‘, ‘55555\r\n‘]
[‘__class__‘, ‘__delattr__‘, ‘__doc__‘, ‘__enter__‘, ‘__exit__‘, ‘__format__‘, ‘__getattribute__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘close‘, ‘closed‘, ‘encoding‘, ‘errors‘, ‘fileno‘, ‘flush‘, ‘isatty‘, ‘mode‘, ‘name‘, ‘newlines‘, ‘next‘, ‘read‘, ‘readinto‘, ‘readline‘, ‘readlines‘, ‘seek‘, ‘softspace‘, ‘tell‘, ‘truncate‘, ‘write‘, ‘writelines‘, ‘xreadlines‘]

readline() 讀取文件一行內容
next() 讀取文件下一行內容返回一個字符串


打開文件步驟:
1、open文件
2、文件操作(讀或者寫)
3、關閉文件

import codecs
f = codecs.open(‘text.txt‘)
text = f.read()
print(type(text))
f.close()
運行結果:
<type ‘str‘>
對字符串操作
import codecs
f = codecs.open(‘text.txt‘)
text = f.read()
print(type(text))
result = text.replace(‘1‘,‘A‘)
print(result)
f.close()
<type ‘str‘>
AAAAA
22222
33333
44444
55555

codecs 這個模塊主要是用來解決文件亂碼問題
open(filename,mode)
mode 有幾個參數需要註意,
r 讀
w 寫
b 二進制
a 追加
write() 必須傳入字符串
writelines() 必須傳入一個序列
import codecs
f = codecs.open(‘2.txt‘,‘ab‘)
f.write("hello world!!\n")
f.write("hello 11111!!\n")
f.write("hello 22222!!\n")
f.write("hello 33333!!\n")
f.write("hello {}!!\n".format("cnblogs"))
f.write("hello %s!!\n" % "cnblogs")
f.close()

file常用方法(上)
讀取文件
f = open("3.txt",‘rb‘)
print(dir(f))
print(f.readlines())
f.close()
運行結果:
[‘__class__‘, ‘__delattr__‘, ‘__doc__‘, ‘__enter__‘, ‘__exit__‘, ‘__format__‘, ‘__getattribute__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘close‘, ‘closed‘, ‘encoding‘, ‘errors‘, ‘fileno‘, ‘flush‘, ‘isatty‘, ‘mode‘, ‘name‘, ‘newlines‘, ‘next‘, ‘read‘, ‘readinto‘, ‘readline‘, ‘readlines‘, ‘seek‘, ‘softspace‘, ‘tell‘, ‘truncate‘, ‘write‘, ‘writelines‘, ‘xreadlines‘]
[‘hello world!!\r\n‘, ‘hello 11111!!\r\n‘, ‘hello 22222!!\r\n‘, ‘hello 33333!!\r\n‘, ‘hello world!!\r\n‘, ‘hello 11111!!\r\n‘, ‘hello 22222!!\r\n‘, ‘hello 33333!!\r\n‘, ‘hello cnblogs!!\r\n‘, ‘hello cnblogs!!‘]

file常用方法(下)
import codecs
f = codecs.open(‘file1.txt‘,‘wb‘)
print(dir(f))
f.write(‘hello world!\ncnblogs\n‘)
print(f.tell())
f.writelines([‘aaa\n‘,‘bbb\n‘,‘ccc\n‘,‘ddd\n‘,‘ddd\n‘])
print(f.tell())
f.seek(0)
f.write(‘this is python!!!!!!!!!!!!‘)
f.close()
運行結果:
[‘__class__‘, ‘__delattr__‘, ‘__doc__‘, ‘__enter__‘, ‘__exit__‘, ‘__format__‘, ‘__getattribute__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘close‘, ‘closed‘, ‘encoding‘, ‘errors‘, ‘fileno‘, ‘flush‘, ‘isatty‘, ‘mode‘, ‘name‘, ‘newlines‘, ‘next‘, ‘read‘, ‘readinto‘, ‘readline‘, ‘readlines‘, ‘seek‘, ‘softspace‘, ‘tell‘, ‘truncate‘, ‘write‘, ‘writelines‘, ‘xreadlines‘]
21
41
file1.txt文件內容:
this is python!!!!!!!!!!!!bb
ccc
ddd
ddd

擴展:
f.name 查看文件的名字
f.closed 布爾值,判斷文件是否關閉
f.encoding() 查看文件的編碼
f.mode 查看文件的打開模式

file的with用法
import codecs
with codecs.open("text.txt","rb") as f :
# print(f.readlines)
print(f.read())
print(f.closed)
print(f.closed)
運行結果:
11111
22222
33333
44444
55555
False
True
text.txt文件內容:
11111
22222
33333
44444
55555
打印帶行號的文件
with codecs.open("text.txt","rb") as f:
for line,value in enumerate(f):
print(line,value)
運行結果:
(0, ‘11111\r\n‘)
(1, ‘22222\r\n‘)
(2, ‘33333\r\n‘)
(3, ‘44444\r\n‘)
(4, ‘55555‘)

讀取指定文件行的內容:
import linecache
count = linecache.getline(filename,linenum)

import linecache
count = linecache.getline("text.txt",4)
print(count)
運行結果:
44444

with方法在我們平時工作中非常重要,簡單方便,支持快速開發,不需要考慮文件關閉的情況,codecs模塊用來解決文件亂碼

(二)2-5 文件操作