[Tutor] need help opening a file in idle

bob gailer bgailer at gmail.com
Sun May 24 07:10:52 CEST 2009


Meital Amitai wrote:
> Hi everyone,
>
> I am not sure how to open a file in my computer in idle.
>
> For my python class the homework states to write a function that 
> accepts the name of a file and returns a tuple containing the number 
> of lines, words and characters in the file. My problem is how do I get 
> idle to first accept the name of a file. I created a file in text edit 
> program saved with a .doc extension, saved on my desktop. However the 
> only way I have learned to open a file in my class is to write 
> something like:
>
> f = open('inputfile','r')
> line = f.readline()
>
> but when i type exactly that into my idle i get the following error 
> message: Traceback (most recent call last):
>   File "<pyshell#12>", line 1, in <module>
>     f = open('inputfile','r')
> IOError: [Errno 2] No such file or directory: 'inputfile'
>
> and when i ask it to open the actual file i created in my text edit 
> program: file1.doc
> f = open('file1.doc','r')
> I get the same message.
>
> I am not sure how to tell it to go to desktop folder in my computer 
> where i saved that file. Am I missing something, and completely not 
> getting the point?

'file1.doc' is a relative path. It looks in the current directory. 
Obviously that file is not in the current directory. So you must provide 
the complete path.

That is no longer a Python issue!

The path to the desktop depends on OS and username!

I suggest you put the file in a different directory; one that you can 
easily specify.


-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list