Python筆記(五)——檔案處理
1.
# 開啟pi_digits.txt檔案
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents) # 在read函式到達檔案末尾會返回一個空字串,顯示出來比原始檔多一個空行
print(contents.rstrip()) # 使用rstrip函式可以刪除這個空行
filename = 'pi_digits.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
filename = 'pi_digits.txt'
with open(filename) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string += line.strip() #刪除空格
print(pi_string)
print(len(pi_string))
filename = 'programming.txt'
with open(filename, 'w') as file_object: #open(filename, 'w')寫入文字並清空已有,open(filename, 'a')附加文字,不清空已有文字
file_object.write("I love programming.")
相關文章
- Python檔案處理-專題筆記Python筆記
- python處理檔案Python
- python檔案處理Python
- python 檔案處理Python
- Python 檔案處理Python
- Python通用程式設計 - 第五章:檔案處理Python程式設計
- python處理txt檔案Python
- Python 批量處理檔案Python
- Python處理大檔案Python
- Python如何處理檔案的?Python
- Python 如何處理大檔案Python
- python ini 配置檔案處理Python
- Python 檔案(file)處理 例子Python
- Python3中檔案處理Python
- 《Python資料處理》讀書筆記Python筆記
- python file 檔案操作筆記Python筆記
- 05 python開發之檔案處理Python
- python筆記-文字處理(第三天)Python筆記
- [R]檔案處理
- bat處理檔案BAT
- bat檔案處理BAT
- Python處理CSV檔案的幾個方法Python
- 【筆記】基於Python的數字影象處理筆記Python
- Python學習筆記|Python之檔案操作Python筆記
- Python學習筆記|Python之特殊檔案Python筆記
- (五)Linux之檔案與目錄管理以及文字處理Linux
- Linux學習之檔案處理命令(二)目錄處理命令 && 檔案處理命令Linux
- Python之錯誤異常和檔案處理Python
- 3.python檔案操作及異常處理Python
- window 批處理檔案
- Go xml檔案處理GoXML
- JAVA ZIP 處理檔案Java
- 批處理檔案命令
- 檔案處理函式函式
- Windows批處理檔案Windows
- bat批處理檔案BAT
- java異常處理筆記Java筆記
- 記一次PMML檔案的處理過程