'OSError: [Errno 63] File name too long while calling python subprocess

While running the following code I'm getting file name too long

import subprocess
import json

data = [{
  "id": 1,
  "first_name": "Janet",
  "last_name": "Tilbury",
  "email": "[email protected]",
  "gender": "Female",
  "ip_address": "181.83.28.51"
}, {
  "id": 2,
  "first_name": "Terrie",
  "last_name": "Reboulet",
  "email": "[email protected]",
  "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)

Following error, I'm getting

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": "[email protected]", "gender": "Female", "ip_address": "181.83.28.51"}, {"id": 2, "first_name": "Terrie", "last_name": "Reboulet", "email": "[email protected]", "gender": "Female", "ip_address": "75.209.60.68"}]'

Is there any way to solve this issue in python?



Solution 1:[1]

The first argument to Popen is supposed to be either

  1. A list, consisting of a program name and its arguments,

  2. A string, containing a program name, or

  3. if shell=True is used, a string containing a shell command.

You seem to be trying to use (3), but without using shell=True. Unless you really need the shell, I recommend going for (1) instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ture PÄlsson