Python problem with instruction open()

In summary, the conversation is about a person trying to run a program in Python but encountering an IOError related to not being able to find the text file 'words.txt'. They discuss different ways to solve this issue, such as using absolute paths, checking the current directory, and using regular expressions. The expert summarizer notes that the paths mentioned are for official Python code, and that the script needs to have a full or relative path directly to the text file in order for it to be found and opened.
  • #1
mynick
34
0
I am fiddling with python v2.7.5,i am using Pyscripter version 2.5.3.0 x86 to write my program and i do not know how to deal with this problem when i run my program:

PROBLEM:

___________________________________________
IOError:[Errno 2] No such file or directory:'words.txt' |
___________________________________________|

My Settings in Pyscripter:
In my Pyscripter menu bar: tools->python path...->here opens a pop up window which says: Ordered list of file paths:
C:\Python27
C:\Python27\lib\site-packages
C:\Python27\lib
(some more...)

I have placed my 'words.txt' in all three file paths i mention above.For example,C:\Python27 it does contain my 'words.txt' among other things.

THE CODE:

N=13
def findme(N):
...i=1
...fin=open('words.txt')
...for i in range(N):
...readaline=fin.readline()
...line=readaline.strip()
...for letter in line:
....if letter!='a':
.....return line

findme(13)

what i want the code to do: open and search my txt file :'words.txt' one line at a time and print on the screen all the words that do not have the letter 'a' in them. The contents of my words.txt are in this fashion:
aaaaaaa
bbbbbbb
assdd
erwe
sddfsdfsfgsgsg

How do i solve the IOError:[Errno 2] No such file or directory:'words.txt' ?

(sorry for the dots in my code,in my pyscripter of course i do not use dots,i used them here to create indents and make it easier to read the code)
 
Last edited:
Technology news on Phys.org
  • #2
It appears it can't find your file. Try using an absolute path to your file like:

C:\mydirectory\mysubdiretory\words.txt (example of a windows path)

or /home/myhome/words.txt

Its probably the case that pyscripter is not using the directory you think its using.

Can you issue commands from within your script like try 'dir' or 'ls -al' command to see what files are listed?

os.system("dir") if windows or os.system("ls -al") if macosx or linux

Alternatively try running a pwd command to see what is the current directory.

Also I would try changing the N used in the findme() to be some other name like 'n'
 
  • #3
os.getcwd() would be what you want to use to find the pwd.

There are also functions to check for the existence of a file/directory that you can use before you call open(), e.g. os.path.exists().

You might also want to look at regular expressions rather than iterating through each character line by line. In a Unix like system you could do the same thing with `grep -v a words.txt'.
 
  • #4
The paths that you listed are where Python searches for Python code / libraries / etc. Those directories are for official Python code, not for application text files. Finding a text file to open and read is different. The open statement needs a full or relative path directly to words.txt. Python will not do any searching for words.txt. It either finds it where you said it is or it doesn't open it. The relative path is relative to where you are running the Python script. The simplest relative path is in the directory where you are running the program. Put words.txt there and your script should be able to open it.
 
  • #5
necro?
 
  • #6
FactChecker said:
The relative path is relative to where you are running the Python script.
Clarification:That is unless the script moves by calling os.chdir(new_working_directory). After that call, the relative paths start at new_working_directory. So you can start a script in one directory and the script can change to more convenient directories to read or write text files. You don't have to move your scripts and text files into the same directory.
 

FAQ: Python problem with instruction open()

What is the "Python problem with instruction open()"?

The "Python problem with instruction open()" refers to an error that occurs when trying to use the "open()" function in Python. This function is used to open files and read or write data. The problem can occur due to a variety of reasons, including incorrect file paths, file permission issues, and incorrect syntax.

How do I fix the "Python problem with instruction open()"?

The solution to the "Python problem with instruction open()" depends on the specific cause of the error. Some common solutions include checking the file path and ensuring it is correct, making sure the file has the correct permissions, and ensuring the correct syntax is used when using the "open()" function.

Why am I getting the "Python problem with instruction open()" error?

There are several reasons why you may encounter the "Python problem with instruction open()" error. These include incorrect file paths, file permission issues, and syntax errors. It is important to carefully check your code and make sure all these factors are correct in order to avoid this error.

Can I prevent the "Python problem with instruction open()" from occurring?

While it is not always possible to prevent this error from occurring, there are steps you can take to reduce the likelihood of encountering it. These include double-checking file paths and permissions before running your code, and using proper syntax when using the "open()" function.

Are there any alternatives to using the "open()" function in Python?

Yes, there are other methods for reading and writing files in Python, such as using the "read()" and "write()" methods or using the "with" statement. These alternatives may be useful if you continue to encounter the "Python problem with instruction open()" error or if you prefer a different approach to file manipulation in your code.

Similar threads

Replies
1
Views
1K
Replies
2
Views
1K
Replies
11
Views
2K
Replies
4
Views
2K
Replies
9
Views
2K
Replies
16
Views
3K
Replies
17
Views
3K
Replies
3
Views
3K
Replies
9
Views
9K
Back
Top