[Tutor] Errno 9

Kent Johnson kent37 at tds.net
Thu Feb 28 15:49:01 CET 2008


imonthejazz wrote:
> Hi All,
> 
> I am trying to read a line in a file then split the words in the line up.
> 
> This is what i thought would work:
> 
> # filearray.py
> 
> import string
> 
> # THE FOLLOWING FOUR LINE ARE THE CORRECT START TO THE PROGRAM,
> # THE SIXTH AND SEVENTH LINES ARE ONLY FOR QUICK TEST PURPOSE!!!
> infilename = raw_input("What file would you like to re-arrange?: ")
> outfilename = raw_input("Where would you like to save to array?: ")
> infile = open(infilename, 'r')
> infile = open(outfilename, 'w')

outfile would be a better name!

> 
> #infile = open("G:\gnet.nmap", 'r')
> #infile = open("G:\work2.txt", 'w')
> 
> # process each line
> for line in infile.readlines():
>     words = string.split(line)
>     print words

Note you haven't written anything to outfile yet; print will go to the 
console. I'm not sure what you do want to write so I won't suggest anything.

> # save and close files
> infile.close()
> infile.close()

I guess this is line 16; you are closing the same file twice. Should be
   outfile.close()

> But i get an "IO Error: [Errno 9] Bad file descriptor" on line 16.

It would help to see the traceback, it includes the line with the error 
so we don't have to guess which is line 16.

Kent


More information about the Tutor mailing list