python - TypeError:bufsize 必须是整数?
问题描述
我正在制作一个小程序,我可以使用它的默认编辑器从计算机的任何部分打开文件。这是我的代码:
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 lib中的split()
方法解析您的命令。像这样的东西:
import subprocess
import shlex
cmd=shlex.split('[find][2] root_dir -name file_name')
print subprocess.check_output(cmd)
推荐阅读
- python - 如何通过多个规则有效地过滤巨大的列表?
- r - 引用对象元素
- windows - 如何以管理员身份运行cmd?但是在命令行中
- c++ - 将对象插入 std::map
- c++ - 如何在 Windows 中读取子进程标准输出?
- algorithm - 当且仅当存在从 u 到 v 的边时,顶点 u 显示在 v 之后的拓扑排序中
- python - 如何在带有变量的python中导入javascript代码?
- c - 从文本文件中读取特定单词
- nginx - 如何在 nginx 中为某些文件扩展名添加带有代理传递的缓存控制标头
- javascript - 为什么我的数组没有填充到 HTTP 请求回调函数中?