【问题标题】:how to read a json file in subprocess and use it in subprocess.call in a python code?如何在 subprocess 中读取 json 文件并在 python 代码中的 subprocess.call 中使用它?
【发布时间】:2019-09-27 23:26:33
【问题描述】:
import subprocess

json=os.startfile("inputs.json")
json = "C:\Users\Onion1\inputs.json"

subprocess.call([json])

尝试了上述方法,但无法打开和调用子进程。

使用 open("C:\Users\O\ss.txt") 作为输出: subprocess.Popen("ls",stdout=out)

【问题讨论】:

  • 你想读取 json 文件并从中执行命令吗?
  • 读取一个json文件并在子进程中使用它
  • 将 Json 文件作为普通文本文件读取并解析并将其内容传递给子进程调用。能否请您出示您要阅读的 Json 文件
  • {"sender": "AB", "body": "Trade order wall movement quite with want large. Store whose magazine ability so interest artist. Owner federal west.\nIt situation take kid money structure. Throughout may whose much or training", "id2": "2119739392053", "id": "111", "device_id": "9878"} 示例 json 包含 1000 个相同格式的 json

标签: python json python-3.x subprocess pipe


【解决方案1】:

如下读取文件,顺便说一句,这不是读取文件的唯一方法。

>>> filename = "inputs.json"
>>> 
>>> fd = open("inputs.json")
>>> json_content = fd.read()
>>> 
>>> fd.close()
>>> 
>>> print(json_content)
{"sender": "AB", "body": "Trade order wall movement quite with want large. Store whose magazine ability so interest artist. Owner federal west.\nIt situation take kid money structure. Throughout may whose much or training", "id2": "2119739392053", "id": "111", "device_id": "9878"}

>>> 
>>> 

使用子进程调用ls -l

>>> import subprocess
>>> 
>>> subprocess.call(["ls", "-l"])
total 4
drwxrwxr-x 3 admin-pc admin-pc 4096 May  7 16:14 cJSON_Test
-rw-rw-r-- 1 admin-pc admin-pc    0 May 10 10:51 inputs.json
0
>>> 

由于我不知道你想使用子进程执行什么进程,所以我以ls -l 为例。我也不清楚你是想将 json 文件内容传递给进程还是 json 文件名。

阅读更多关于子进程here

【讨论】:

  • 是否有 1024 个计数或 2048 个计数的限制??大 json 文件无法发送子进程调用
  • 您是否将 json 文件内容传递给 subprocess.call?
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2021-10-21
  • 2020-02-15
  • 2015-02-19
相关资源
最近更新 更多