Python 子进程模块相当于 Windows 中的双击

发布于 2024-10-07 14:30:11 字数 342 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一遍 2024-10-14 14:30:11
os.startfile(r'C:\myfile.csv')

(仅限 Win32。对于 Mac,使用 'open filename' 运行进程;在 Linux/freedesktop-in-general 上,使用 'xdg-open filename'。)

os.startfile(r'C:\myfile.csv')

(Win32 only. For Mac, run a process with 'open filename'; on Linux/freedesktop-in-general, 'xdg-open filename'.)

不交电费瞎发啥光 2024-10-14 14:30:11

我认为你的部分问题是你使用 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 program C:, 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.

菊凝晚露 2024-10-14 14:30:11

我知道这有点晚了,但是要在 python 2.x(不确定 3)中执行此操作,您应该使用 subprocess 模块,引用 Popen。这是代码:

import subprocess
subprocess.Popen(r'explorer /select, "C:\"')

它基本上打开文件,然后在默认程序中打开它。

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, referencing Popen. Here is the code:

import subprocess
subprocess.Popen(r'explorer /select, "C:\"')

It basically opens the file, and then opens it in the default program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文