file1=open('test.txt
','w+
') #
覆蓋原有內容寫入,指標處於檔案開頭(開啟乙個檔案用於讀寫。如果檔案存在,刪除重新編輯,否則新建寫入)
file1.write('
test1
') #
在開啟的test.txt中寫入test1內容
file1.close() #
關閉檔案file1,使用 open() 方法一定要保證關閉檔案物件,即呼叫 close() 方法
file1=open('
test.txt
','a+
') #
在原有內容後追加寫入一行或多行,指標處於原有內容末尾處,不覆蓋原有內容
file1.write('
\ntest1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7')
file1.close()
file1=open('
test.txt
','r+
') #
開啟乙個檔案用於讀寫(檔案的指標將在檔案開頭)
print(file1.read()) #
讀取檔案內容,讀取完成後指標處於檔案末尾
file1.close()
file1=open('
test.txt
','r+')
print(file1.read(2)) #
read(n)讀取指定位元組長度的內容,算的的是字元
file1.close()
file1=open('
test.txt
','r+')
print(file1.readline()) #
readline()讀取首行,讀取完成後指標處於首行末尾
print(file1.readline()) #
讀取下一行
print(file1.readline()) #
多個readline()時,一行一行讀取
file1.close()
file1=open('
test.txt
','r+')
print(file1.readlines()) #
讀取多行內容,返回到列表中,此時指標在檔案末尾處
file1.close()
file1=open('
test.txt
','r+')
print(file1.readlines()[3]) #
讀取多列表中的第四[3]個元素,既某行元素
print(file1.readlines()) #
此時指標在檔案末尾,列印出來的為空列表
file1.close()
file1=open('
test.txt
','r+')
for x in
file1.readlines():
(x)file1.close()
file1=open('
test.txt
','r+')
print(file1.read(7)) #
列印讀取七個字元,test1\n', 'test1\n'
print(file1.tell()) #
讀取七個元素,指標處於第8個位置
file1.close()
file1=open('
test.txt
','r+')
file1.readline()
#test1\n
print('
獲取指標位置:
',file1.tell())
file1.readline()
#test1\n
print('
獲取指標位置:
',file1.tell()) #
'test1\n', 'test1\n', 'test2\n', 'test3\n', 'test4\n', 'test5\n', 'test6\n', 'test7'
file1.close()
file1=open('
test.txt
','r+')
print(file1.read(14)) #
列印14個字元
file1.close()
file1=open('
test.txt
','w+')
file1.write(
'test1')
file1.close()
file1=open('
test.txt
','r+')
print('1.'
,file1.read())
file1.close()
file1=open('
test.txt
','a+')
file1.write(
'\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7')
file1.close()
file1=open('
test.txt
','r+')
print('
2.\n
',file1.read())
file1.close()
file1=open('
test.txt
','r+')
print(file1.readlines()) #
['test1\n', 'test2\n', 'test3\n', 'test4\n', 'test5\n', 'test6\n', 'test7']
file1.seek(4,0) #
從檔案頭開始,偏移數為4,從0開始,4為第5個數,既為1
print('
3.',file1.readline()) #
讀取當前行指標後的字元,既1
file1.seek(0,0) #
偏移量為0,既指標回到檔案開頭
print('
4.',file1.read(6)) #
列印6個字元
file1.close()
#with open(路徑,模式) as 變數:
#**塊
with open(
'test.txt
','r
') as filen: #
檔案進行操作,開啟檔案儲存到filen中
print(filen.read())
python 關於檔案操作的一些理解
在用python進行資料處理程式設計中,往往涉及到檔案io口讀寫,io口的讀寫效能會極大的影響程式的執行時間。在進行檔案寫入時,一般會存在兩種情況。第一種是資料到來馬上進行資料寫入,即來一條寫一條,第二種是資料到來時先儲存到記憶體中,等到資料量儲存到一定程度時,再一次性寫入檔案中。那麼,這兩種情況孰...
關於檔案的一些操作
今天遇到一些關於檔案的知識點 codeblocks16.0裡面不支援 include只有 include使用時要把std包包含在裡面 using namespace std 而vc6.0卻不能使用這個包 下面是課件上的一些知識點,以後用到的時候再拿出來看看吧 include void main in...
關於python的一些特性
首先,我其實得說說為什麼我們會選擇python。在我加入企業快盤團隊之前,整個專案包括更早的金山快盤都是採用python進行開發的。至於為什麼這麼選擇,當時的架構師蔥頭告訴我,主要是因為python上手簡單,開發迅速。對於團隊裡面大部分完全沒服務端開發經驗的同學來說,python真的是乙個很好的選擇...