我正在尝试在 python 脚本中执行 ps1
文件。
proc = subprocess.Popen(["powershell.exe", 'F:/FOLDER/FOLDER OK/myfile.ps1'])
它停在F:/FOLDER/FOLDER
,所以我添加了额外的引号,如下所示:
proc = subprocess.Popen(["powershell.exe", '"F:/FOLDER/FOLDER OK/myfile.ps1"'])
现在没有错误,但它也没有执行脚本... 有什么想法吗?
最佳答案
powershell.exe
并不假定参数自动是要执行的文件;您需要识别所有参数。使用-File
开关
proc = subprocess.Popen(["powershell.exe", "-File", r"F:\FOLDER\FOLDER OK\myfile.ps1"])
r"..."
中的 r
(对于 raw)会关闭转义序列处理,以便 \
无需转义即可使用。
参见PowerShell Command Line Help在 Microsoft 文档。
PowerShell[.exe]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]
[-EncodedCommand <Base64EncodedCommand>]
[-ExecutionPolicy <ExecutionPolicy>]
[-File <FilePath> [<Args>]]
[-InputFormat {Text | XML}]
[-Mta]
[-NoExit]
[-NoLogo]
[-NonInteractive]
[-NoProfile]
[-OutputFormat {Text | XML}]
[-PSConsoleFile <FilePath> | -Version <PowerShell version>]
[-Sta]
[-WindowStyle <style>]
关于python - 执行绝对路径包含空格的powershell文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49110935/