[英]Opening file vs reading file in python
我正在嘗試打印文件,使用兩種方法按行拆分:一種是使用讀取文件的方法,第二種是使用 for 循環並將文件拆分成行。 我在最后一行收到一個回溯錯誤,指出“單詞”未定義。 我不明白為什么會這樣。
fname = input('enter file name')
try:
fhandle = open(fname, 'r')
except:
print('file does not exist')
exit()
#store entire file in a variable called data
data = fhandle.read()
print(data)
#iterate through each line in a file handle
for line in fhandle:
line = line.strip()
words = line.split()
print(words)
讀取文件時,Python 會跟蹤文件中的 cursor。 數據從 cursor 的 position 向前讀取,讀取將 cursor 向前移動到讀取數據的末尾。 這樣,例如,調用f.readline()
兩次將每次都返回下一行,而不是兩次都返回第一行。
當您調用f.read()
時,會讀取整個文件,因此 cursor 會移動到文件末尾。 然后,當您遍歷fhandle
時, Python 只考慮 cursor 前面的行 - 其中沒有。 由於被迭代的 object 是空的,因此永遠不會執行 for 循環的主體,因此永遠不會分配words
。
您可以通過在 for 循環之前直接調用fhandle.seek(0)
來解決此問題,以將 cursor 返回到文件的開頭。
您的程序中也存在邏輯錯誤。 如果要在 for 循環中打印每一行,而不僅僅是最后一行,則需要縮進print(words)
以便它在 for 循環中。
作為最佳實踐,您還應該在使用完文件后調用fhandle.close()
。
您的文件可能是空的,這就是導致問題的原因。 您也可以使用variable = fhandle.readlines()
它會返回一個列表。 我也建議關閉文件。
注意:盡管使用 4 空格縮進是最佳實踐,但正如其他人所說,您的代碼中沒有縮進、scope 或邏輯錯誤。
由於read()
未定義的words
,它使 for 循環沒有返回任何內容。
Python 文件方法 read() 從文件中讀取最多 size 個字節。 如果讀取在獲得 size 字節之前達到 EOF,則它只讀取可用字節。
當print(words)
在 for 循環中縮進時,它也不返回任何內容。 但是如果在print(words)
沒有縮進的情況下刪除了read()
,它將返回最后一行的列表:
fname = input('enter file name')
try:
fhandle = open(fname, 'r')
except:
print('file does not exist')
exit()
# store entire file in a variable called data
# data = fhandle.read()
# print(data)
# iterate through each line in a file handle
for line in fhandle:
line = line.strip()
words = line.split()
print(words)
# ['Line', '4']
如果print(words)
在read()
被刪除時縮進,它將返回:
fname = input('enter file name')
try:
fhandle = open(fname, 'r')
except:
print('file does not exist')
exit()
# store entire file in a variable called data
# data = fhandle.read()
# print(data)
# iterate through each line in a file handle
for line in fhandle:
line = line.strip()
words = line.split()
print(words)
# ['Line', '1']
# ['Line', '2']
# ['Line', '3']
# ['Line', '4']
我不確定您使用split()
的意圖是什么,但如果您只想使用read()
逐行打印,那么您的代碼已經做到了。
使用 for 循環時,只需注釋或刪除read()
然后打印line
fname = input('enter file name')
try:
fhandle = open(fname, 'r')
except:
print('file does not exist')
exit()
# store entire file in a variable called data
# data = fhandle.read()
# print(data)
# iterate through each line in a file handle
for line in fhandle:
print(line.strip())
# Line 1
# Line 2
# Line 3
# Line 4
但是如果你打算制作一個由每一行組成的列表,你可以使用splitlines()
fname = input('enter file name')
try:
fhandle = open(fname, 'r')
except:
print('file does not exist')
exit()
#store entire file in a variable called data
data = fhandle.read().splitlines()
print(data)
# ['Line 1', 'Line 2', 'Line 3', 'Line 4']
希望這有幫助。
問題是您沒有縮進print(words)
行。 變量words
的 scope 位於 for 循環內。 嘗試這個
fname = input('enter file name')
try:
fhandle = open(fname, 'r')
except:
print('file does not exist')
exit()
#store entire file in a variable called data
#data = fhandle.read()
#print(data)
#iterate through each line in a file handle
for line in fhandle:
line = line.strip()
words = line.split()
print(words)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.