Python subprocess failing due to TypeError: bufsize must be an integer

Viewed 34
I have a command where it should execute in the path which I have given as input in the subprocess. I'm getting the following error when tried to execute it. 
command = "bazel run //ros/src/bag_to_yaml:bag_to_yaml -- "
command = command + " ".join(tracks_ids)

print(command)

path1 = "/home/terli.vaibhav/development/github.robot.car/cruise/cruise/develop"
p = subprocess.Popen(command, path1 ,bufsize=1, shell = True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

This is the error I'm getting TypeError: bufsize must be an integer.

Traceback (most recent call last):
  File "example.py", line 45, in <module>
    execute_subprocess(output_dir, segment, vai)
  File "example.py", line 31, in execute_subprocess
    p = subprocess.Popen(command, path1 ,shell = True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  File "/usr/lib/python3.6/subprocess.py", line 629, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
1 Answers

When using shell=True you have to put the whole command line as the first argument. The second argument happens to be bufsize. So your code is passing path1 as bufsize and you are getting that error. You probably want to do this instead:

cmdline = command + ' ' + path1
p = subprocess.Popen(cmdline,bufsize=1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

By the way:

  • You are specifying shell=True for nothing, because your code doesn't use any shell feature. this opens your code to security vulnerabilities for nothing.
  • Depending on what the subprocess does you can use subprocess.check_output instead of Popen to get the output from a subprocess.

So I'd rewrite your code as something like this:

path1 = "/home/terli.vaibhav/development"
cmdline = ['bazel', 'run', '//ros/src/bag_to_yaml:bag_to_yaml', '--'] + track_ids + [path1]
output = subprocess.check_output(cmdline)
Related