【问题标题】:launch an exe with parameter from python从 python 启动一个带有参数的 exe
【发布时间】:2018-08-23 09:46:06
【问题描述】:

我正在尝试调用应用程序并将文本文件作为参数传递。文本文件有说明。我尝试在打开记事本时传递一个参数,但无法让它工作。这是我正在尝试的代码:

import os

os.startfile(r"C:\Windows\notepad.exe c:\cobra\advancecalendar.txt")

If I don't include the path to the second file it launches notepad, but with the file name included i get the following error:

RESTART: C:/Users/timbo/AppData/Local/Programs/Python/Python36-32/Open exe module.py 
Traceback (most recent call last):
  File "C:/Users/timbo/AppData/Local/Programs/Python/Python36-32/Open exe module.py", line 3, in <module>
    os.startfile(r"C:\Windows\notepad.exe c:\cobra\advancecalendar.txt")
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Windows\\notepad.exe c:\\cobra\\advancecalendar.txt'

如何将参数传递给 startfile 以打开特定文件?当我尝试将两者都放在引号中时,它仍然出现错误。

【问题讨论】:

  • 尝试多搜索一下,我想这个问题经常被问到。
  • 只对文件使用os.startfile(),不使用应用程序。
  • 谢谢。我没有看到一个带参数的,但可能会缩小我的搜索范围会再次挖掘

标签: python exe parameter-expansion


【解决方案1】:

使用subprocess 模块。

import subprocess
subprocess.call([r"C:\Windows\notepad.exe", "c:\cobra\advancecalendar.txt"])

python2.7

中测试

【讨论】:

  • 谢谢,这应该可以解决我也遇到的另一个问题。
  • 不客气。如果它解决了您的问题,请接受。谢谢
猜你喜欢
  • 2015-08-25
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
相关资源
最近更新 更多