Python Forum

Full Version: read/print from file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to print a file based on this tutorial, (basic)files, and some other suggestions I found online.

I get nothing in return!
with open('12days.txt', 'r') as fin:
    file = fin.readline()
    print (file)
As you know readline() reads from the file until 'new line' character is reached.

In [7]: print('\n')



In [8]: print(r'\n')
\n

In [9]:
In order to print all file you have to put readline() into a loop.

with open('12days.txt', 'r') as fin:
    for file in fin.readline():
        print(file)
It still returns nothing.
PS C:\Users\User\mystuff> python 12days.py
PS C:\Users\User\mystuff>
What is the length of the 12days.txt file?
The length is 2,232 characters.
try:
with open('12days.txt', 'r') as fin:
    for line in fin.readlines():
        print(line)
Note readlines, not readline
(Dec-29-2016, 03:40 AM)wavic Wrote: [ -> ]In order to print all file you have to put readline() into a loop.
No,just iterate over file-object.
with open('12days.txt') as fin:
   for file in fin:
       print(file)
(Dec-29-2016, 04:27 AM)Larz60+ Wrote: [ -> ]try:
with open('12days.txt', 'r') as fin:
    for line in fin.readlines():
        print(line)
Note readlines, not readline

Thanks! It works!
Smile

readline() !!! At 5am I have to keep my mouth shut. I was almost asleep  Sick