Python 子进程模块相当于 Windows 中的双击
我想使用 subprocess
模块打开一个文件,就像在资源管理器中双击该文件一样。我该怎么做?
我尝试了以下行:
subprocess.call("C:/myfile.csv", shell=True)
它抛出一个错误:
该命令的语法是 不正确。
'C:\' 未被识别为 内部或外部命令, 可运行的程序或批处理文件。
如何使用 subprocess
模拟双击?基本上我想在 Excel 2007 中打开 CSV 文件。
I want to open a file using the subprocess
module as though the file was double-clicked in Explorer. How do I do that?
I tried the following line:
subprocess.call("C:/myfile.csv", shell=True)
which throws an error saying:
The syntax of the command is
incorrect.
'C:\' is not recognized as
an internal or external command,
operable program or batch file.
How do I emulate a double-click using subprocess
? Basically I want to open a CSV file in Excel 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(仅限 Win32。对于 Mac,使用
'open filename'
运行进程;在 Linux/freedesktop-in-general 上,使用'xdg-open filename'
。)(Win32 only. For Mac, run a process with
'open filename'
; on Linux/freedesktop-in-general,'xdg-open filename'
.)我认为你的部分问题是你使用 unix 风格的斜杠 / 作为路径分隔符,而不是 windows 反斜杠。看起来 Windows 正在将
/myfile.csv
解释为程序C:
的参数,这就是您收到该消息的原因。但是,如果您更正了这一点,我想您只会得到它说
C:\myfile.csv
不是一个程序。I think part of your problem is you're using a unix style slash / as a path separator, instead of the windows backslash . It looks like windows is interpreting
/myfile.csv
as an argument for the programC:
, which is why you're getting that message.However if you corrected that, I think you'd just get it saying that
C:\myfile.csv
isn't a program.我知道这有点晚了,但是要在 python 2.x(不确定 3)中执行此操作,您应该使用
subprocess
模块,引用Popen
。这是代码:它基本上打开文件,然后在默认程序中打开它。
I know that this is a bit late, but to do that in python 2.x (not sure about 3) you should use the
subprocess
module, referencingPopen
. Here is the code:It basically opens the file, and then opens it in the default program.