【问题标题】:Using subprocess in Python, I get different results when in Python cmd and Python IDLE在 Python 中使用子进程,在 Python cmd 和 Python IDLE 中我得到不同的结果
【发布时间】:2018-07-05 12:29:03
【问题描述】:

我希望标题有意义。具体来说:

我正在使用 csvtotable (https://github.com/vividvilla/csvtotable) 从 CSV 生成 HTML 表格。我已经通过 pip 安装并且能够运行命令行命令:

csvtotable test1743.csv test1743.html

生成 HTML 页面。到目前为止一切顺利。

我想在我已经编写的 Python 脚本中执行此操作,因此我听说 subprocess 是执行此操作的方法。我查找了如何做到这一点,并了解可以使用以下方法完成:

subprocess.run('csvtotable test1743.csv test1743.html',shell=True)

所以我首先通过命令行测试了这个

python

从命令行然后运行

import subprocess
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)

成功!有效。太棒了。

但是,当我尝试从 IDLE 执行此操作时,它只返回一个 1。我检查了目录,认为那里可能缺少 csv,但它仍然不起作用。

我是否误解了子流程的工作原理?

【问题讨论】:

  • 你试过subprocess.run(["csvtotable", "test1743.csv", "test1743.html"], shell=True)吗?
  • @GuiGWR 我刚刚尝试过,不幸的是它返回了CompletedProcess(args=['csvtotable', 'test1743.csv', 'test1743.html'], returncode=1)。我将在子流程中处理输入和输出参数并报告。
  • 也许你应该使用communicate 看看子进程的输出是什么
  • 这里有一个解释链接:stackoverflow.com/a/16770371/9802108> 和一种操作标准输出的方法
  • 谢谢,我去看看。

标签: python command-line subprocess


【解决方案1】:

通过找到一种在没有子进程的情况下调用函数的方法来解决。我认为这个问题可能与通过 python 执行时未设置默认参数有关,因此为什么下面我必须指定这么多参数。

代码:

from csvtotable import convert

content = convert.convert("C:\\Users\\admin\\Google Drive\\test1743.csv",delimiter=",",quotechar='"',display_length=-1,overwrite=False,serve=False,pagination=True,virtual_scroll=1000, no_header=False, export=True, export_options=["copy","csv","json","print"])
convert.save("C:\\Users\\admin\\Google Drive\\test1743.html",content)

请注意,必须更改参数名称中包含- 的位置。我只是更改了任何实例,例如在 convert.py 中 display-lengthdisplay_length

【讨论】:

    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2019-01-06
    • 2017-10-28
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多