繁体   English   中英

从另一个目录中的另一个脚本运行脚本

[英]Run script from another one in different directory

我正在尝试从其他目录中的app.py运行results.py 我有以下映射:

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

results.py

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

我试图更改工作目录,但仍然有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()

快速修复可以

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

我正在尝试从其他目录中的app.py运行results.py

您可以在路径“结果/”文件中创建__init__.py:

#file __init__.py

from .results import get_file

然后在app.py中,您可以这样称呼:

import results

results.get_file()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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