Jump to content

file opening error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
when I try to load a file using
fin=open("text.txt")

I get this error

Traceback (most recent call last):
File "C:/Python25/test.py", line 17, in <module>
fin=open("text.txt")
IOError: [Errno 2] No such file or directory: 'text.txt'


I know the file does exist, ive tried swapping between putting the file in my documents and the desktop.
Its driving me mad!

#2
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
Maybe try
fin = open("text.txt", r)

Is the txt file and the python file in the same folder? Try including the full path if not.
Posted Image

#3
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Yea that was it cheers. I cant believe I missed something so blindingly obvious

#4
pedro3005

pedro3005

    Newbie

  • Members
  • PipPip
  • 14 posts
At least here (Linux), Python allows you to omit the "r", as it defaults to that. If you add an "w", as in open("a.txt", "w"), it will create the file if it doesn't exist.

#5
Guest_x42_*

Guest_x42_*
  • Guests
the reason why is that, "text.txt" is not the full name, you see, first you have to put "c:\" which is the drive for it to look in, then you have to put in instructions for what files to go through to ge to your file, so if you are running windows xp: "c:\Documents and Settings\your_user_name\My Documents\text.txt" this means that in the C drive there is a folder that is not in any other folders that is called "Documents and Settings" in it there is one to go into that is titled with your user name, then there is a folder in it called "My Documents" and in that there is a file called "text" and it has the file extension ".txt"