導航:首頁 > 編程語言 > python文件寫

python文件寫

發布時間:2022-02-23 13:54:39

A. python 文件讀寫

你的filename是多少,看提示貌似是你的文件不存在活著路徑錯誤

B. Python如何讀寫文本文件

1.open使用open打開文件後一定要記得調用文件對象的close()方法。比如可以用try/finally語句來確保最後能關閉文件。
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
註:不能把open語句放在try塊里,因為當打開文件出現異常時,文件對象file_object無法執行close()方法。
2.讀文件讀文本文件input = open('data', 'r')
#第二個參數默認為r
input = open('data')

讀二進制文件input = open('data', 'rb')
讀取所有內容file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
讀固定位元組file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )
讀每行list_of_all_the_lines = file_object.readlines( )
如果文件是文本文件,還可以直接遍歷文件對象獲取每行:
for line in file_object:
process line
3.寫文件寫文本文件output = open('data.txt', 'w')
寫二進制文件output = open('data.txt', 'wb')
追加寫文件output = open('data.txt', 'a')

output .write("\n都有是好人")

output .close( )

寫數據file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )

C. python文件寫操作

這樣把
教你個簡單的辦法
python一般不會在原文件中操作的,一般會讀出來,操作,然後再寫入的。代碼如下:
#encoding:gbk
insert='123'
#你想插入的字元串
line=''
#最終文件內容
f=open("1.txt","r")
i=f.readline()
#讀取文件內容
f.close()
pre=i[0:3]
last=i[3:]
line=pre+insert+last
f=open("1.txt","w")
f.write(line)
f.close()

D. python向文件內寫入數據

f=open("a.txt","w")
foriinrange(1,10):
f.write("<user> <id>"+str(i)+"</id> </user> ")
f.close()

因為i是int型,所以要先轉為str型,再進行字元串拼接,然後寫入文件

E. python中打開文件再寫入的兩種方式的區別

沒有區別;
均為可讀可寫。
如果文件不存在就創建一個新文件。
看你習慣用哪種

F. python文件讀寫

這兩個問題都是由於最後一句使用了to_excel導致的,改成to_csv即可
另外,to_csv可以生成csv或者xls文件。
改成:data.to_csv(cleanedfile,sep="\t", encoding="utf-8")

G. PYTHON寫文件 寫不進去 也不報錯

你的寫模式錯誤。用w模式,每次會覆蓋原來的,所以,你只會得到最後寫入的東西,而你最後寫入了一個換行。想要追加寫入,用a模式

H. 怎樣用python寫文件

這個應該打開之後有會有讓你讓你寫字的,然後你就可以把你想寫的寫上。

I. python怎麼以追加的方式寫文件

一、用Python創建一個新文件,內容是從0到9的整數, 每個數字佔一行:

#python

>>>f=open('f.txt','w') # r只讀,w可寫,a追加

>>>for i in range(0,10):f.write(str(i)+' ')

. . .

>>> f.close()

二、文件內容追加,從0到9的10個隨機整數:

#python

>>>import random

>>>f=open('f.txt','a')

>>>for i in range(0,10):f.write(str(random.randint(0,9)))

. . .

>>>f.write(' ')

>>>f.close()

三、文件內容追加,從0到9的隨機整數, 10個數字一行,共10行:

#python

>>> import random

>>> f=open('f.txt','a')

>>> for i in range(0,10):

. . . for i in range(0,10):f.write(str(random.randint(0,9)))

. . . f.write(' ')

. . .

>>> f.close()

四、把標准輸出定向到文件:

#python

>>> import sys

>>> sys.stdout = open("stdout.txt", "w")

J. 關於python里寫文件的操作

  1. fo.close()---你小括弧沒有。

  2. fo = open("D:/text一.txt","w",encoding = 'utf-8')
    ls = ['13','14','15']
    fo.writelines(ls)
    fo.close()

  3. 文件在d盤下面

閱讀全文

與python文件寫相關的資料

熱點內容
好多照片壓縮一個文件夾怎麼弄 瀏覽:139
伺服器如何創建站點 瀏覽:4
鄞州山姆附近啟蒙編程哪家好 瀏覽:98
解壓好玩的手工玩具 瀏覽:877
idea是c語言的編譯器嗎 瀏覽:545
單片機p4口優缺點 瀏覽:226
自動機學習演算法 瀏覽:422
為什麼源代碼顯示不出編譯 瀏覽:571
pdf的文件怎麼列印 瀏覽:56
安卓系統國產怎麼樣 瀏覽:69
端游命令方塊在哪 瀏覽:967
導數運演算法則構造函數 瀏覽:206
我的世界網易版伺服器怎麼導入 瀏覽:449
jpg圖片壓縮小 瀏覽:847
上饒公交app怎麼坐不了公交 瀏覽:475
廣西獲客系統源碼 瀏覽:137
張鐸程序員 瀏覽:420
全民解壓音樂60秒 瀏覽:525
當選定文件夾後 瀏覽:583
h3c和華為命令 瀏覽:367