Python學習筆記———《文件操作》

判斷文件是否存在

import os
filename = '~/lcj/test.txt'
if os.path.exists(filename):
    message = 'OK, the "%s" file exists.'
else:
    message = "Sorry, I cannot find the "%s" file."
print message % filename

讀取文件指定行內容

filename = '~/lcj/test.txt'
row_num = 10
content = linecache.getline(filename,row_num)

一次性讀取所有的內容放在一個大字符串中

all_the_text=open('test.txt').read()



發佈了26 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章