我是python的新手,在我的第一个程序中,我正在尝试从FLAC文件中提取元数据以重命名它们。
我的代码的这个特殊部分给我带来了一些麻烦:
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 =”。
问题是,当我运行它时,我得到了回复:
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ébutd'laFin”怎么可以是一个整数......
答案 0 :(得分:2)
subprocess.call
返回一个整数(退出代码),而不是输出。
使用stdout
参数,然后致电Popen.communicate()
:
pipe = subprocess.Popen(
["metaflac", "--show-tag=title", filename], stdout=subprocess.PIPE)
title, error = pipe.communicate()
答案 1 :(得分:1)
该输出可能来自您的子流程。
subprocess.call
返回返回码,而不是stdout上的输出。
答案 2 :(得分:0)
subprocess.call返回进程的退出代码,而不是输出。为了获得输出,您需要为参数stdout传递一个值(according to the documentation,可以是PIPE,现有文件描述符(正整数),现有文件对象和无)。
This thread有更多关于替代(以及更好的IMO)方法的信息来实现这一目标。