python - 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] 没有这样的文件或目录
解决方案
os.system()
当第一次执行终止时不会记住目录的更改,因此您的解决方案无法正常工作。
正如评论中所建议的,您可以省略使用绝对路径cd
执行的命令:pret.py
import os
def test():
os.system('python2 <absolute_path>/PRET/pret.py')
test()
推荐阅读
- python - Python cv2与主图像不匹配模板
- azure-devops - 大型 Azure DevOps(和 Azure DevOps Server 2019)变更集因“请求实体太大”而失败
- c++ - 无法在 Windows 10 上的 Win32 C++ 控制台应用程序中检测 BluetoothLE 设备
- r - 基于基本 R 连接函数 c() 编写 S4 方法。省略号、点的使用,
- php - WordPress - 仅显示与登录用户相关的帖子
- python - 为什么熊猫模块方法 df.to_csv 显示权限错误
- pyspark - 在另一个 spark sql 查询中使用 PySpark Dataframe 列
- python - 在 Python 的 C func 中,浮点变量的格式不正确
- r - 用 mutate_at 替换值总是给出行数不匹配的错误,如何解决这个问题?
- css - webpack HtmlWebpackPlugin 不会创建链接 css 标签