Python Forum
read/print from file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: read/print from file (/thread-1377.html)



read/print from file - mcmxl22 - Dec-29-2016

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)



RE: read/print from file - wavic - Dec-29-2016

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)



RE: read/print from file - mcmxl22 - Dec-29-2016

It still returns nothing.
PS C:\Users\User\mystuff> python 12days.py
PS C:\Users\User\mystuff>



RE: read/print from file - Larz60+ - Dec-29-2016

What is the length of the 12days.txt file?


RE: read/print from file - mcmxl22 - Dec-29-2016

The length is 2,232 characters.


RE: read/print from file - Larz60+ - Dec-29-2016

try:
with open('12days.txt', 'r') as fin:
    for line in fin.readlines():
        print(line)
Note readlines, not readline


RE: read/print from file - snippsat - Dec-29-2016

(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)



RE: read/print from file - mcmxl22 - Dec-29-2016

(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!


RE: read/print from file - wavic - Dec-29-2016

Smile

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