当我运行这个
file_path = convertfilepath("$temp/x.bat","windows");
savetextfile(file_path, "dir\!n");
rp = RunProgram( Executable( "cmd.exe" ),
Options( {"/C", file_path} ),
ReadFunction("text") // returns all text
);看来执行 bat 文件的工作目录是我的桌面目录。 我认为 python 也会使用工作目录在命令行上查找文件名。 使用完整路径名似乎是一个不错的选择。
https://superuser.com/questions/396394/how-do-i-set-an-executables-working-directory-via-the-command-line-prior-to-ex建议您可以在启动命令中指定工作目录:
START /D c:\temp notepad.exe
我遇到的问题应该与我正在执行的嵌套调用有关。
RunProgram() --> cmd --> Python
下面的bat文件工作得很好。
set JMP_PYTHON_PTH=%UserProfile%\miniconda_JMP\
CALL %JMP_PYTHON_PTH%\Scripts\activate.bat %JMP_PYTHON_PTH%
start /B /wait "" python test.py
然而,当使用 RunProgram() 时,除非我添加 Python 文件的绝对路径(test.py --> 'C:/a/b/c/test.py'),否则代码会失败
ClearLog();
file_path = ConvertFilePath( "./tests/run_python_installation_test.bat", windows);
rp = RunProgram( Executable( "cmd.exe" ),
Options( {"/C", file_path} ),
ReadFunction("text") // returns all text
);仅当未给出绝对路径时,我得到的错误如下。
(base) C:\WINDOWS\system32>start /B /wait \!"\!" python test.py
python: can't open file 'test.py': [Errno 2] No such file or directory