[英]python 2.7 reading a last empty line from a text file
我正在讀取一個文件並基於以下代碼的分隔符寫入一個新文件
dataFile = sys.argv[1]
def fileread(inputfile):
with open(inputfile,'r') as f:
lines = f.readlines()
for line in lines:
print "Inside for"
print line
def read_master_file(dataFile):
try:
with open(dataFile) as fmaster:
op=''
start=0
for seperator in fmaster.read().split('\n'):
#print x
if(seperator=="####"):
if(start == 1):
with open("data.txt",'w') as fdata:
#print op
fdata.seek(0,2)
fdata.write(op)
fdata.close()
fileread("data.txt")
#sys.exit(1)
op=''
else:
start = 1
else:
if (op ==''):
op = seperator
else:
op = op + '\n' + seperator
fmaster.close()
except Exception as e:
print "File not found", e
read_master_file(dataFile)
如果輸入文件包含一個 \\n ,它會被添加到 data.txt 但是當我嘗試讀取它時 \\n 值在它是最后一行時沒有得到反映
1,2
2,3
3,4
如果我理解你的問題,那么做
lines = f.readlines()[-1]
然后您將能夠閱讀最后一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.