python - 类型错误 : bufsize must be an integer?

标签 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

我已经用谷歌搜索了这个错误的解决方案,但我找不到任何似乎可以解决我的问题的方法。如何解决我的错误?

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

最佳答案

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

import subprocess
import shlex

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

关于python - 类型错误 : bufsize must be an integer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529415/

相关文章:

python - python扩展模块中的C++静态成员初始化

linux - UDP 服务器出现段错误

Linux 中的 Python OSError no 7(参数列表太长)

python - Google Earth Engine Python API 的解决方法和 Python 3 中不支持 `ee.mapclient`

python - 为什么 numpy.trapz() 返回零?

python - Python 中的阿拉伯语 Wordnet 同义词?

python - 执行多个函数,跟踪是否有返回 true 的函数

python - GDB 崩溃 - 意外的 GDB STDERR : Fatal Python error: Py_Initialize: Unable to get the locale encoding

python - 选择键来跟踪递归调用中已经运行的函数

检查 TCP 套接字上的传入接口(interface)