[英]Not printing file contents
I'm trying to read and print the contents of a text file, but nothing shows up:我正在尝试读取和打印文本文件的内容,但没有显示任何内容:
coffee = open('coffeeInventory.txt' , 'r')
coffee.seek(0)
line = coffee.readline()
while line != '':
print(line)
coffee.close()
Thank you for any advice.谢谢你的任何建议。
Try this:尝试这个:
with open('coffeeInventory.txt') as inf:
for line in inf:
print(line, end='')
readline
leaves a newline on the end of the line, so use end=''
to prevent print
from appending its own newline. readline
在行尾留下一个换行符,所以使用end=''
来防止print
附加自己的换行符。
Try this code for each line, please:请为每一行尝试此代码,请:
file = open('coffeeInventory.txt')
lines = file.readlines()
for line in lines:
print(line)
file.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.