TypeError: bufsize must be an integer?

10,626

Instead, use subprocess.check_output(). Since your command has multiple words, parse your command with split() method from shlex lib. Something like this:

import subprocess
import shlex

cmd=shlex.split('[find][2] root_dir -name file_name')
print subprocess.check_output(cmd)
Share:
10,626
Mahir Islam
Author by

Mahir Islam

I am just a high-school student who enjoys programming :)

Updated on June 13, 2022

Comments

  • Mahir Islam
    Mahir Islam about 2 years

    I am making a small program where I can open a file from any part of the computer with it's default editor. This is my code:

    from os import *
    import subprocess
    print("Welcome to my File Finder. Here you can search for a file and open it.")
    file_name = str(input("Your file's name:"))
    print(subprocess.call(["xdg-open"], file_name))]
    

    But instead of opening, it return this error:

    Traceback (most recent call last):
    File "Important_tester_projects.py", line 6, in <module>
      print(subprocess.call(["xdg-open"], file_name))
    File "/usr/lib/python3.6/subprocess.py", line 267, in call
      with Popen(*popenargs, **kwargs) as p:
    File "/usr/lib/python3.6/subprocess.py", line 609, in __init__
      raise TypeError("bufsize must be an integer")
    TypeError: bufsize must be an integer
    

    I have googled to find a solution for this error, but I can't find any that seems to solve my Problem. How can fix my error?

    NOTE: My Linux OS uses XFCE, not Gnome.

    • abarnert
      abarnert almost 6 years
      This is a simple typo: instead of passing ["xdg-open", file_name] as the args, you’re passing ["xdg-open"]` as the args and file_name as the bufsize.
    • Mahir Islam
      Mahir Islam almost 6 years
      @abarnert what should I pass then?
    • aydow
      aydow almost 6 years
      @mahir, he means use call like this print(subprocess.call(["xdg-open"], file_name))
    • aydow
      aydow almost 6 years
    • Mahir Islam
      Mahir Islam almost 6 years
      @aydow it still returns error
    • abarnert
      abarnert almost 6 years
      Pass ["xdg-open", file_name], just as I said. I guess this isn't a simple typo, and you don't understand the API?
    • abarnert
      abarnert almost 6 years
      @aydow No I don't. That's exactly what he's already calling, so it's going to cause the exact same error he's already getting.
    • aydow
      aydow almost 6 years
      sorry, my mistake. i meant print(subprocess.call(["xdg-open", file_name]))
    • abarnert
      abarnert almost 6 years
      @aydow Since that question is itself a dup of bufsize must be an integer error while grepping a message, probably better to use the latter as a dup target, unless there's something wrong with it. (I already voted to close as a typo and retracted, so I can't vote to close as a dup…)
    • aydow
      aydow almost 6 years
      @abarnert, i did see that, but i think that stackoverflow.com/questions/34156193/… better addresses this question.
    • abarnert
      abarnert almost 6 years
      @aydow OK, cool. If I hadn't wasted my vote, I'd vote for your dup. :)
    • aydow
      aydow almost 6 years
      @abarnert no worries :)