首页 > 解决方案 > sh: line 0: cd: PRET: No such file or directory error in Python

问题描述

我试图使用 Python 编写一个脚本,该脚本使用了一个名为 PRET 的开源笔测试工具,该工具是我从 Github 获得的。我为此使用 PyCharm。

我的代码是:

import os
def test():
    os.system('cd PRET')
    os.system('python2 pret.py')
test()

这样做时,我遇到了这个错误:

sh:第0行:cd:PRET:没有这样的文件或目录/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python:无法打开文件'pret.py ': [Errno 2] 没有这样的文件或目录

标签: python

解决方案


os.system()当第一次执行终止时不会记住目录的更改,因此您的解决方案无法正常工作。

正如评论中所建议的,您可以省略使用绝对路径cd执行的命令:pret.py

import os

def test():
    os.system('python2 <absolute_path>/PRET/pret.py')

test()

推荐阅读