0

I have been trying to read a file in python, the thing is that it returns an empty string. Here is the code:


with open('data.txt') as file:
    content = f.readlines()
    print(content) #prints nothing

note: It's a really big file, is that a problem?

6
  • hmmm just did it but its still the same Commented Jun 5, 2022 at 17:39
  • Are you confident that there are some data inside the data.txt file? If-Then Can you provide the data and some more code? Commented Jun 5, 2022 at 17:40
  • yeah, there is data Commented Jun 5, 2022 at 17:41
  • 1
    is it a problem if the file is 4tb? Does that overwork the program? Commented Jun 5, 2022 at 17:41
  • 1
    Fixed: the file was too big Commented Jun 5, 2022 at 17:46

4 Answers 4

2
  1. change f.readlines() to file.readlines() This is only a typo. But I don't know why you don't get the error here. I think you open another file as f And You get the empty string here 'cause if you try to read a file more than once, you got an empty string except for the first time.
  2. As you say the file size is 4TB, This could be the problem because of Max File Size In Python
Sign up to request clarification or add additional context in comments.

Comments

0

You need to make sure your text file name is the proper one, but most important, you need to provide full path of the file, in case it is not in the same directory as the script.

2 Comments

It is in the same directory. I wonder whats the problem :(
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

So I had this same issue with when I read a file using with open, as all it would do was return nothing even though the txt file was in the same directory and was full of content.

file_path = r"C:\Users\windowsuser\Downloads\folder1\folder2\textfile.txt"
    
try:
    file = open(file_path, "r", encoding='utf-8')
    content = file.read()
    print("File contents (visible):\n", content)
        
except FileNotFoundError:
    print("File not found.")
except Exception as e:
    print("Error:", e)
finally:
    if 'file' in locals():
        file.close()

Comments

-2

with open('data.txt') as file:
    content = file.readlines()
    print(content) #prints nothing


This should work i think

2 Comments

it still dosent work
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.