OSError: [Errno 63]调用python子进程时文件名过长



在运行以下代码时,我得到的文件名太长

import subprocess
import json
data = [{
"id": 1,
"first_name": "Janet",
"last_name": "Tilbury",
"email": "jtilbury0@cocolog-nifty.com",
"gender": "Female",
"ip_address": "181.83.28.51"
}, {
"id": 2,
"first_name": "Terrie",
"last_name": "Reboulet",
"email": "treboulet1@hud.gov",
"gender": "Female",
"ip_address": "75.209.60.68"
}]
bashCommand = 'java -cp . AESwithRSAEncryption %s' %json.dumps(data)
process = subprocess.Popen(bashCommand, stdout=subprocess.PIPE)
output, error = process.communicate()
print(output, error)

下面的错误,我得到

Traceback (most recent call last):
File "encrypt.py", line 20, in <module>
process = subprocess.Popen(bashCommand, stdout=subprocess.PIPE)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 63] File name too long: 'java -cp . AESwithRSAEncryption [{"id": 1, "first_name": "Janet", "last_name": "Tilbury", "email": "jtilbury0@cocolog-nifty.com", "gender": "Female", "ip_address": "181.83.28.51"}, {"id": 2, "first_name": "Terrie", "last_name": "Reboulet", "email": "treboulet1@hud.gov", "gender": "Female", "ip_address": "75.209.60.68"}]'

有没有办法在python中解决这个问题?

Popen的第一个参数应该是

  1. 由程序名及其参数组成的列表,

  2. 字符串,包含程序名或

  3. 如果使用shell=True,包含shell命令的字符串。

你似乎试图使用(3),但没有使用shell=True。除非您真的需要shell,否则我建议您使用(1)。

最新更新