0

I have a problem with a code in python. I want to read a .txt file. I use the code:

f = open('test.txt', 'r')  # We need to re-open the file
data = f.read()

print(data)

I would like to read ONLY the first line from this .txt file. I use

f = open('test.txt', 'r')  # We need to re-open the file
data = f.readline(1)

print(data)

But I am seeing that in screen only the first letter of the line is showing.

Could you help me in order to read all the letters of the line ? (I mean to read whole the line of the .txt file)

1
  • After f = open('test.txt', 'r'), then one_line = next(f). Commented Aug 22, 2020 at 9:10

5 Answers 5

5
with open("file.txt") as f:
   print(f.readline())

This will open the file using with context block (which will close the file automatically when we are done with it), and read the first line, this will be the same as:

f = open(“file.txt”)
print(f.readline())
f.close()

Your attempt with f.readline(1) won’t work because it the argument is meant for how many characters to print in the file, therefore it will only print the first character.

Second method:

with open("file.txt") as f:
   print(f.readlines()[0])

Or you could also do the above which will get a list of lines and print only the first line.

To read the fifth line, use

with open("file.txt") as f:
   print(f.readlines()[4])

Or:

with open("file.txt") as f:
   lines = []
   lines += f.readline()
   lines += f.readline()
   lines += f.readline()
   lines += f.readline()
   lines += f.readline()
   print(lines[-1])

The -1 represents the last item of the list

Learn more:

Sign up to request clarification or add additional context in comments.

1 Comment

I would like to read ONLY the 5th line. How could I do this?
0

Your first try is almost there, you should have done the following:

f = open('my_file.txt', 'r')
line = f.readline()
print(line)
f.close()

A safer approach to read file is:

with open('my_file.txt', 'r') as f:
    print(f.readline())

Both ways will print only the first line.

Your error was that you passed 1 to readline which means you want to read size of 1, which is only a single character. please refer to https://www.w3schools.com/python/ref_file_readline.asp

3 Comments

I would like to read ONLY the 5th line. How could I do this?
@IvanRoma just iterate 5 times on the file, if you will call the readline method 5 times you will get the 5th line, or f.readlines()[4]
please leave a comment explaining why you downvoted my answer, thanks.
0

I tried this and it works, after your suggestions:

f = open('test.txt', 'r')
data = f.readlines()[1]

print(data)

2 Comments

You can “accept” or validate my (or someone else’s) answer if it was useful, just click the tick below the vote buttons - stackoverflow.com/help/accepted-answer
Also please remember to f.close() to close the file when you are done with it, check my answer for more details
-1

Use with open(...) instead:

with open("test.txt") as file:
    line = file.readline()
    print(line)

Comments

-1

Keep f.readline() without parameters.

It will return you first line as a string and move cursor to second line.

Next time you use f.readline() it will return second line and move cursor to the next, etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.