-1

I have a text file named hsp.txt in C:\Python27\Lib\site-packages\visual\examples and used the following code.

def file():
    file = open('hsp.txt', 'r')
    col = []
    data = file.readlines()
    for i in range(1,len(data)-1):
        col.append(int(float(data[i].split(',')[5])))
    return col

def hist(col):
    handspan = []
    for i in range(11):
        handspan.append(0)
    for i in (col):
        handspan[i] += 1
    return handspan

col = file()
handspan = hist(col)
print(col)
print(handspan)

But when I run it it says that that the file doesn't exist.

Traceback (most recent call last):
  File "Untitled", line 17
    col = file()
  File "Untitled", line 2, in file
    file = open('hsp.txt', 'r')
IOError: [Errno 2] No such file or directory: 'hsp.txt'

How do I fix this? Also how do I output the mean and variance?

Jeffrey
  • 121
  • 2
  • 3
  • 1
    Type this in: `os.getcwd()`. What does it return? – Zizouz212 May 17 '15 at 01:15
  • 1
    @MartijnPieters you're a saint for spending so much time on all these terrible questions. – abcd May 17 '15 at 01:21
  • @Jeffrey you should really explicitly close the file when you're done with it -- or open the file with a `with` block. – abcd May 17 '15 at 01:22
  • 1
    @dbliss: really, just because the post comes from someone not yet familiar with all the ins and outs of formatting on this site doesn't make the question terrible in and of itself. They did provided what they tried and the full error message. That's pretty good going in my book! – Martijn Pieters May 17 '15 at 01:24
  • 1
    @MartijnPieters good point. my only disappointment is that the OP didn't google the error message -- this would have revealed plenty of answers. – abcd May 17 '15 at 01:27
  • i had a similar problem, it turned out my file names were too long. – enter_thevoid Sep 01 '21 at 14:23

2 Answers2

2

Have you thought where your path leads to? You need to supply the complete path to the file.

opened_file = open("C:/Python27/Lib/site-packages/visual/examples/hsp.txt")

A couple other things:

  • Don't use file as a variable name. The system already uses that name.

Use a with statement. It's considered better practice.

with open("C:/Python27/Lib/site-packages/visual/examples/hsp.txt"):
    # do something

When the with block ends, the file is automatically closed. In your code, the file is left open until the file function is closed (and hence saved) with the .close() method.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
1

When your specifying just the following line

    file = open('hsp.txt', 'r')

It is trying to use your current directory, that is where ever you launched python from. So if you, from a command prompt were at C:\temp and executed python test.py it woud look for your hsp.txt in C:\temp\hsp.txt. You should specify the full path when your not trying to load files from your current directory.

   file = open(r'C:\Python27\Lib\site-packages\visual\examples\hsp.txt')
Brian Cain
  • 946
  • 1
  • 7
  • 20