繁体   English   中英

不打印文件内容

[英]Not printing file contents

我正在尝试读取和打印文本文件的内容,但没有显示任何内容:

coffee = open('coffeeInventory.txt' , 'r')
coffee.seek(0)
line = coffee.readline()
while line != '':
    print(line)
    
coffee.close()

谢谢你的任何建议。

尝试这个:

with open('coffeeInventory.txt') as inf:
    for line in inf:
        print(line, end='')

readline在行尾留下一个换行符,所以使用end=''来防止print附加自己的换行符。

请为每一行尝试此代码,请:

file = open('coffeeInventory.txt') 
lines = file.readlines()         
for line in lines:
    print(line)
file.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM