TypeError:bufsize必须是整数?

时间:2018-07-26 00:31:56

标签: python linux python-3.x

我正在制作一个小程序,可以用它的默认编辑器从计算机的任何部分打开文件。这是我的代码:

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))]

但是它没有打开而是返回此错误:

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

我已经在Google上寻找解决此错误的方法,但找不到任何似乎可以解决我的问题的方法。如何解决我的错误?

注意:我的Linux操作系统使用XFCE,而不是Gnome。

1 个答案:

答案 0 :(得分:1)

请使用subprocess.check_output()。由于您的命令有多个单词,因此请使用shlex lib中的split()方法来解析命令。 像这样:

import subprocess
import shlex

cmd=shlex.split('[find][2] root_dir -name file_name')
print subprocess.check_output(cmd)