0

I have a program that relies on user input to enter files for the program to open in Python 2.7.11. I have all of those files in a sub-directory called TestCases within the original directory Detector, but I can't seem to access the files in TestCases when running the program from the super-directory. I tried to use os.path.join but to of no avail. Here is my code:

import os.path
def __init__(self):
    self.file = None
    os.path.join('Detector', 'TestCases')

    while self.file == None:
        self.input = raw_input('What file to open? ')
        try:
            self.file = open(self.input, 'r')
        except:
            print "Can't find file."

My terminal when I run the program goes as follows:

>>> What file to open? test.txt # From the TestCases directory
>>> Can't find file.
>>> What file to open? ...

Am I using os.path.join incorrectly? I thought it was supposed to link the two directories so that files could be accessed from the sub-directory while running the program from the super-directory.

1
  • you are trying to open the file in the current directory, not in the TestCases dir Commented Apr 24, 2016 at 7:46

2 Answers 2

1

You are using os.path.join('Detector', 'TestCases'), that should return 'Detector/TestCases', but you aren't storing that variable anywhere.

I suppose that you are in Detector directory and you want to open files in TestCases. I that case you can use path join (It concatenates its arguments and RETURNS the result):

    import os.path

    file = None
    while not file:
        input = raw_input('What file to open? ')
        try:
            filepath = os.path.join('TestCases', input)
            file = open(filepath, 'r')
        except IOError:
            print "Can't find " + input

I have stored the result of os.path.join so you could see that it doesn't change the directory, it just concatenates its arguments, maybe you was thinking that function will change the directory, you can do it with os.chdir.

Try it first in a simple script or in the terminal, it will save many headaches.

Sign up to request clarification or add additional context in comments.

1 Comment

If this errors out, your script is likely not in the Director directory, thus I suggest you supplying the full path to the TestCases directory and just join it with input as on the example by Jose J
1

The documentation about os.path.join

Join one or more path components intelligently. The return value is the concatenation of path...

It seems like you expect it to set some kind of PATH variable or affect the current working directory. For a first start it should be sufficient to add something like this to your code:

open(os.path.join("TestCases",self.input), 'r')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.