简体   繁体   中英

Run script from another one in different directory

I am trying to run results.py from app.py located in a different directory. I have the following mapping:

App/
---model/
--------model.csv
---results/
----------results.py
---app.py

results.py

def get_file():
    df = pd.read_csv('../model/model.csv')
    ...

I tried to change the working directory but I still have FileNotFoundError

app.py

curr_dir = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(curr_dir, 'results')
subprocess.Popen("ls", cwd=path)
get_file()

A quick fix can be

def get_file():
    try:
        df = pd.read_csv('../model/model.csv')
    except FileNotFoundError:
       df = pd.read_csv('./model/model.csv')

I am trying to run results.py from app.py located in a different directory

you can create in your path 'results/' file __init__.py:

#file __init__.py

from .results import get_file

and then in app.py you can call this:

import results

results.get_file()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM