python學習筆記07 檔案

2021-10-05 14:28:49 字數 4287 閱讀 3208

現在開始學習檔案了,其實前面的學習中還有不少遺漏的地方,以後我會繼續改進,加油

1.3逐行讀取

1.4建立乙個包含檔案各行內容的列表

1.5使用檔案內容

2.寫入檔案

with

open

('hello.txt'

)as file_object:

text=file_object.read(

)print

(text)--

----

----

----

----

----

----

hello python

how are you?

you are so easy

你可能會看到輸出的資料有多餘的空行,如果有,則參照如下**

with

open

('hello.txt'

)as file_object:

text=file_object.read(

)print

(text.rstrip())

----

----

----

----

hello python

how are you?

you are so easy

如果你要開啟的檔案不在當前**所在的資料夾,你需要明確它的路徑

file_path=

r'c:\users\dell\desktop\hello.txt'

with

open

(file_path)

as file_object:

text=file_object.read(

)print

(text.rstrip())

----

----

----

----

----

hello python

how are you?

you are so easy

記得絕對路徑單引號前加r第一次我沒加,然後報錯

with

open

('text_files\hello.txt'

)as file_object:

text=file_object.read(

)print

(text.rstrip())

----

----

----

----

---hello python

how are you?

you are so easy

with

open

('text_files\hello.txt'

)as file_object:

for line in file_object:

print

(line)--

----

----

----

----

-hello python,

how are you?

you are so easy.

去掉多餘空行

with

open

('text_files\hello.txt'

)as file_object:

for line in file_object:

print

(line.rstrip())

----

----

----

----

---hello python,

how are you?

you are so easy.

with

open

('text_files\hello.txt'

)as file_object:

lines=file_object.readlines(

)for line in lines:

print

(line.rstrip())

print

("讀取列表中的元素"

)print

(lines[1]

)---

----

----

----

----

hello python,

how are you?

you are so easy.

讀取列表中的元素

how are you?

with

open

('text_files\hello.txt'

)as file_object:

lines=file_object.readlines(

)hello_string=

' 'for line in lines:

hello_string+=line.rstrip(

)print

(hello_string)

print

(len

(hello_string))-

----

----

----

----

--hello python,how are you?you are so easy.

42

with

open

('text_files\hello.txt'

,'w'

)as file_object:

file_object.write(

'i love python.'

)with

open

('text_files\hello.txt'

)as file_object:

text=file_object.read(

)print

(text)--

----

----

----

----

i love python.

with

open

('text_files\hello.txt'

,'w'

)as file_object:

file_object.write(

'i love python.'

) file_object.write(

'i love hadoop.'

) file_object.write(

'i love r.'

)with

open

('text_files\hello.txt'

)as file_object:

text=file_object.read(

)print

(text.title())

----

----

----

----

i love python.i love hadoop.i love r.

你可以加入換行符讓每個字串獨佔一行

這樣寫入會直接覆蓋原有檔案!!!謹慎操作

如果你只是想在文字的內容後面新增內容,則使用以下方法

with

open

('text_files\hello.txt'

,'a'

)as file_object:

file_object.write(

'i am here.'

) file_object.write(

'i get you.'

) file_object.write(

'go for it.'

)with

open

('text_files\hello.txt'

)as file_object:

text=file_object.read(

)print

(text.title())

----

----

----

----

i love python.i love hadoop.i love r.i am here.i get you.go for it.

Python學習筆記 6 檔案

要開啟的檔案應該儲存在你執行的python程式同乙個資料夾下。這個檔案儲存在你啟動python時所在的那個資料夾。fhand open mbox.txt print fhand 如果檔案成功被開啟,作業系統會返回乙個檔案控制代碼。如果檔案不存在,開啟失敗,輸出追蹤錯誤資訊。文字檔案可視為若干文字行的...

Python學習筆記9 檔案

在python中,要對乙個檔案進行操作,只需用內建的open函式開啟檔案即可。signature open file,mode r buffering 1,encoding none,errors none,newline none,closefd true,opener none docstrin...

Python學習筆記 四 檔案操作

讀取鍵盤輸入 buf raw input please input your name buf raw input 開啟檔案 如果hello.txt不存在 fp open hello.txt w w是說建立這個檔案,以寫的方式開啟 fp.write text fp.close 如果hello.txt...