我有一个GUI,并且我正在使用一个按钮来调用python脚本。
我使用python os.path.abspath(os.path.dirname(__file__))
来获取图形用户界面脚本的目录,并进一步使用它来调用位于同一目录下的子文件夹中的脚本。
我使用以下命令获取GUI的路径:
sPfad = os.path.abspath(os.path.dirname(__file__))
print(sPfad)
T:\kst597\common\FB\Reporting\Web\Datenladung in SAP
我在这里存储了我想调用的脚本的路径:
feld_script_man = sPfad+"\Felddaten\Konverter_Claims_MAN\Konverter_Felddaten_MAN.py"
我使用以下命令调用脚本:
os.system("python "+feld_script_man+" 1")
我得到的错误是:
python: can't open file 'T:\kst597\common\FB\Reporting\Web\Datenladung': [Errno 2] No such file or directory
我还确保路径存在:
print(os.path.exists(sPfad+"\Felddaten\Konverter_Claims_MAN\Konverter_Felddaten_MAN.py"))
True
我能做些什么来解决这个问题呢?
发布于 2019-07-31 11:48:37
是的,os.path.exists
返回True
,但在os.system
中不同,因为命令行/参数解析发生了:
os.system("python "+feld_script_man+" 1")
从字面上扩展为
os.system("python T:\kst597\common\FB\Reporting\Web\Datenladung in SAP\Felddaten\Konverter_Claims_MAN\Konverter_Felddaten_MAN.py 1")
正如您所看到的,没有引号,所以python尝试打开第一个参数T:\kst597\common\FB\Reporting\Web\Datenladung
,但失败了。
始终使用subprocess
模块(os.system
已弃用),并始终使用参数列表。
这应该是可行的:
subprocess.check_call(["python",sPfad+r"\Felddaten\Konverter_Claims_MAN\Konverter_Felddaten_MAN.py","1"])
https://stackoverflow.com/questions/57289760
复制相似问题