嘗試使用subprocess.Popen()以文件路徑作爲參數運行命令。這失敗了,似乎Popen()將當前目錄添加到傳遞給命令的參數列表中。Python Popen將當前目錄添加到參數?
[email protected]:~/Downloads/pycharm-community-2016.3.2/bin$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> vm_root = "/mas_data/vms/"
>>> vm_path = "\"" + vm_root + "XP VM 1/XP VM 1.vmx\""
>>> vm_path
'"/mas_data/vms/XP VM 1/XP VM 1.vmx"'
>>> subprocess.Popen(["vmrun", "start", vm_path])
<subprocess.Popen object at 0x7f6b9225c898>
>>> Error: Cannot open VM: /home/phil/Downloads/pycharm-community-2016.3.2/bin/"/mas_data/vms/XP VM 1/XP VM 1.vmx", unknown file suffix
exit()
[email protected]:~/Downloads/pycharm-community-2016.3.2/bin$ vmrun start "/mas_data/vms/XP VM 1/XP VM 1.vmx"
<VM launches>
不知道我很想念得到POPEN工作,提前執行命令vmrun start "/mas_data/vms/XP VM 1/XP VM 1.vmx"
謝謝!
請勿將引號中的路徑包圍。當使用'Popen'時,沒有必要這樣做,引號只會導致路徑無效。 –
當你只傳遞單個字符串參數時,你需要在'Popen'中。 –