Search code examples
pythoneclipseeclipse-neon

Call a file in another folder in Eclipse for Python project


I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:

I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.

enter image description here

enter image description here


Solution

  • It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?

    It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder

    Groovy("folder_name/test.txt")
    

    You can also go up in the directory structure if you need to, for example

    Groovy("../folder_name/test.txt")
    

    Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.

    import os
    
    filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
    u = Groovy(filename)