My .txt file to open is in the same directory as the .ipynb (Python) file.
Probably there are some problems with the permissions or something. How could we Solve this?
The best practice here is to use “With open file” so you won’t have to bother about closing files and error handling.
# Read the whole file in a string
with open('test.txt', 'r') as f:
data = f.read()
print(data)
# Or put the lines in a list
with open('test.txt', 'r') as f:
data = f.read().splitlines()
print(data)
You could also use f.readline() to read
Not worth reading, still here for reference.
Thanks for your question. Can you be more specific?
what is your os / anaconda version
what is the exact code you are trying to run?
what is the content of your file?
edit:
from what folder are you trying to run your code?
can you make a screenshot of the Jupyter file list like below?
My steps to reproduce
Create file “test.txt” with some random content in the same directory as the Jupyter Notebook file
edit: I tested with a space in the folder name, and that doesn’t make a difference here. Everything is still working as expected. Can’t seem to reproduce your problem so far.
The content was like you wrote.
Probably the edited .txt file (with content) wasn’t saved before i ran the program.
So the saved (default) version i wanted to open was ofcourse empty.
I tried to run the program a couple minutes later and now the .txt was autosaved which made the program run.
My problem is solved!