10

Currently I am trying to open a text file called "temperature.txt" i have saved on my desktop using file handler, however for some reason i cannot get it to work. Could anyone tell me what im doing wrong.

#!/Python34/python
from math import *

fh = open('temperature.txt')

num_list = []

for num in  fh:
    num_list.append(int(num))

fh.close()
4
  • 2
    Please show us the error you get. A short description of your data in the text file would also be helpful. Commented Oct 17, 2016 at 23:23
  • FileNotFoundError - tempertature.txt should be in the same directory as your .py file. ValueError - trying to convert a string ( read from file ) to int but it's not an int. Can't see output - print your list. Commented Oct 18, 2016 at 5:46
  • There is nothing wrong with your code. If you want help - you'll need to give us the error as well. As in previous comments though, the problem is mostly likely that the file does not exist (or the script is looking in the wrong directory for it) or there are lines in your file that aren't just numbers (for example, whitespace at the end of the file may cause an error in the for loop even though the file opened correctly) Commented Oct 19, 2016 at 23:42
  • @StevenSummers "tempertature.txt should be in the same directory as your .py file." - No, it should be in the current working directory, which is where the interpreter was launched from - and this is not necessarily where the script is. Commented Feb 24, 2024 at 19:33

2 Answers 2

12

The pythonic way to do this is

#!/Python34/python

num_list = []

with open('temperature.text', 'r') as fh:
    for line in fh:
        num_list.append(int(line))

You don't need to use close here because the 'with' statement handles that automatically.

If you are comfortable with List comprehensions - this is another method :

#!/Python34/python

with open('temperature.text', 'r') as fh:
    num_list = [int(line) for line in fh]

In both cases 'temperature.text' must be in your current directory.

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

5 Comments

You missed a quote after the filename in both examples.
@shadow - easily fixed with a quick edit :-) - all done.
That's much better :)
@shadow - The convention on this site is to do minor edits like that yourself - so long as the edit corrects an error. I think you have enough rep now to edit other people's posts now. The edits will be reviewed so you can't break anything .... But thanks for letting me know - much appreciated.
Usually the site doesn't let you make such minor edits, and I feel that letting the original poster know helps them for next time. But thanks for reminding me I have that ability.
1

You simply need to use .readlines() on fh

like this:

#!/Python34/python
from math import *

fh = open('temperature.txt')

num_list = []

read_lines = fh.readlines()
for line in read_lines:
    num_list.append(int(line))

fh.close()

3 Comments

You mean for line in lines
This isn't actually true. Calling readlines will load the whole file into memory - but leaving it alone will only have one line in memory a time making it more efficient with bigger files.
as @shadow has said - using 'readlines' isn't neccessary. simply doing 'for num in fh' will iterate around each line in the file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.