當我運行這個
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