Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python problem with instruction open()

  1. Oct 16, 2013 #1
    I am fiddling with python v2.7.5,i am using Pyscripter version x86 to write my program and i do not know how to deal with this problem when i run my program:


    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:
    (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.


    def findme(N):
    .......for i in range(N):
    ...........for letter in line:
    .................if letter!='a':
    .....................return line


    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:

    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: Oct 16, 2013
  2. jcsd
  3. Oct 16, 2013 #2


    Staff: Mentor

    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'
  4. Oct 16, 2013 #3


    User Avatar
    Gold Member

    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'.
  5. Jul 4, 2014 #4


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
  6. Jul 4, 2014 #5


    User Avatar

    Staff: Mentor

  7. Jul 5, 2014 #6


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Python problem with instruction open()
  1. Python problem (Replies: 3)

  2. N-body problem in Python (Replies: 10)