Python:AttributeError:'int'对象没有属性'replace' [英] Python: AttributeError: 'int' object has no attribute 'replace'
问题描述
我是 Python 新手,在我的第一个程序中,我试图从 FLAC 文件中提取元数据以重命名它们.
I'm new to python and my in my first program I'm trying to extract metadata from FLAC files to rename them.
我代码的这个特殊部分给我带来了一些麻烦:
This particular part of my code is causing me some trouble:
import subprocess
filename = raw_input("Path?")
title = subprocess.call(
["metaflac", "--show-tag=title", filename])
new_title = title.replace("TITLE=", "")
print new_title
'metaflac --show-tag=title file.flac' 发回TITLE=foo",我试图摆脱TITLE=".
'metaflac --show-tag=title file.flac' sends back "TITLE=foo" and I'm trying to get rid of "TITLE=".
问题是,当我运行这个时,我得到了这个:
The problem is, when I run this, I get this back:
TITLE=foo
Traceback (most recent call last):
File "test.py", line 16, in <module>
title = title.replace("TITLE=", "")
AttributeError: 'int' object has no attribute 'replace'
我只是不明白字符串TITLE=Début d'la Fin"怎么可以是一个整数...
I just don't understand how can the string "TITLE=Début d'la Fin" can be an integer...
推荐答案
subprocess.call
返回一个整数(退出代码),而不是输出.
subprocess.call
returns an integer (the exit code), not the output.
使用 stdout
参数,并调用 Popen.communicate()
:
Use the stdout
argument, and call Popen.communicate()
:
pipe = subprocess.Popen(
["metaflac", "--show-tag=title", filename], stdout=subprocess.PIPE)
title, error = pipe.communicate()
这篇关于Python:AttributeError:'int'对象没有属性'replace'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!