6

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()
AER
  • 1,549
  • 19
  • 37
Alex Osborne
  • 69
  • 1
  • 1
  • 2
  • 2
    Please show us the error you get. A short description of your data in the text file would also be helpful. – RichArt Oct 17 '16 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. – Steven Summers Oct 18 '16 at 05: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) – Shadow Oct 19 '16 at 23:42

2 Answers2

9

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.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
  • You missed a quote after the filename in both examples. – Shadow Oct 19 '16 at 23:39
  • @shadow - easily fixed with a quick edit :-) - all done. – Tony Suffolk 66 Oct 20 '16 at 06:30
  • That's much better :) – Shadow Oct 20 '16 at 22:21
  • @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. – Tony Suffolk 66 Oct 21 '16 at 07:12
  • 1
    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. – Shadow Oct 23 '16 at 22:51
0

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()
Moardant
  • 23
  • 7
  • You mean for line in lines – B. Kostas Oct 18 '16 at 02:00
  • 2
    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. – Shadow Oct 18 '16 at 03:23
  • as @shadow has said - using 'readlines' isn't neccessary. simply doing 'for num in fh' will iterate around each line in the file. – Tony Suffolk 66 Oct 18 '16 at 08:06