Why isn’t my file reading output showing up in Jupyter Lab?

0
1
Asked By CuriousCat98 On

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

Answered By TechyTom77 On

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.

CuriousCat98 -

Good point! I’ll check the file content next. Thanks!

Answered By LearningNinja99 On

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!

CuriousCat98 -

Thanks for clarifying! I’ll make sure to use print going forward.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.