Hey everyone! I'm new to programming and I'm trying to read a list from a file using Jupyter Lab. I have a file named 'testreading.txt' in the same directory as my notebook. When I use this code:
```python
with open('testreading.txt') as file:
file.readlines()
```
I don't see any output in the notebook. I also tried:
```python
f = open("testreading.txt", "r")
print(f.readlines())
```
This gives me the output, but it's not displayed vertically like I expected. I'm really confused about why I'm not getting any output without using the print function. Any help would be super appreciated, even if it seems simple!
2 Answers
Have you checked the contents of 'testreading.txt'? If the file is empty, all you’ll get is an empty list. You might also consider opening the file in read mode using `with open(...) as file:` for better practice, which automatically handles closing the file for you.
It looks like you're trying to read the lines from your file, but by default, calling `file.readlines()` without printing it will not display anything in the notebook. You need to explicitly print the result to see it. That's why your second attempt with `print(f.readlines())` shows the output. If you want that vertical look, you'll still need to use `print` here!
Thanks for clarifying! I’ll make sure to use print going forward.
Good point! I’ll check the file content next. Thanks!