Frage

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' 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'

I just don't understand how can the string "TITLE=Début d'la Fin" can be an integer...

War es hilfreich?

Lösung

subprocess.call returns an integer (the exit code), not the output.

Use the stdout argument, and call Popen.communicate():

pipe = subprocess.Popen(
     ["metaflac", "--show-tag=title", filename], stdout=subprocess.PIPE)
title, error = pipe.communicate()

Andere Tipps

That output is coming presumably from your subprocess.

subprocess.call returns the return code, not the output on stdout.

subprocess.call returns the exit code of the process, not the output. In order to get the output you need pass a value for the parameter stdout (which, according to the documentation, can be PIPE, an existing file descriptor (a positive integer), an existing file object, and None).

This thread has more information on alternate (and better, IMO) methods to accomplish this.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top