Home > Blockchain >  Not printing file contents
Not printing file contents

Time:10-23

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.

CodePudding user response:

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.

CodePudding user response:

Try this code for each line, please:

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