类型错误:缓冲区大小必须是整数?

3

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

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。


3
这只是一个简单的打字错误:你传递参数时,应该传入 ["xdg-open", file_name],但你传入了 ["xdg-open"] 作为参数,并将 file_name 作为缓冲区大小。 - abarnert
@abarnert 那我应该传递什么呢? - Mahir Islam
抱歉,我的错误。我想说的是 print(subprocess.call(["xdg-open", file_name])) - aydow
1
@abarnert,我看到了,但我认为https://dev59.com/RlsX5IYBdhLWcg3wLM3K更好地回答了这个问题。 - aydow
@abarnert 不用担心 :) - aydow
显示剩余7条评论
1个回答

2

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

import subprocess
import shlex

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接