0
votes

I'm trying to define a function to be used in a bigger program, but when I run this, I get this output:

Traceback (most recent call last): File "test.py", line 29, in print tracknumber NameError: name 'tracknumber' is not defined

The code runs fine if I don't write it as a function (without def)

Any idea?

import subprocess

filename = raw_input("Path?")

def metadata():

    pipe = subprocess.Popen(
        ["metaflac", "--show-tag=tracknumber", filename], stdout=subprocess.PIPE)
    tracknumber, error = pipe.communicate()
    tracknumber = tracknumber.replace("TRACKNUMBER=", "")    
    tracknumber = tracknumber.rstrip()
    if int(tracknumber) < 10:
        tracknumber = "0" + tracknumber

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

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

metadata()

print tracknumber
print artist
print title
3
you need to learn about scope of a variable. I hope this link will help you - OOkhan

3 Answers

1
votes

You need to return values from the function and assign them to variables in global scope, otherwise tracknumber, artist, title are going to raise NameError as they don't exist in global scope.

A function can access variables defined in global scope(filename), but variables defined inside a function can't be accessed outside of it.

def metadata():

    pipe = subprocess.Popen(
        ["metaflac", "--show-tag=tracknumber", filename], stdout=subprocess.PIPE)
    tracknumber, error = pipe.communicate()
    ...
    ... 
    artist = artist.rstrip()
    return tracknumber, artist, title  #return variables at end

tracknumber, artist, title = metadata()

print tracknumber
print artist
print title
1
votes

The variables that you create inside of a function are local to that function, so when you try to access them outside of the function they won't be found and you will get an error.

Here are two options on how to fix this:

  • Return the values that you need to use outside of the function and let the caller handle them:

    def metadata():
        # all of your current code
    
        # add this to the bottom
        return tracknumber, artist, title
    
    # save the returned values to global variables
    tracknumber, artist, title = metadata()
    
  • Use the global statement so that assignment to the variables within your function is actually assigning to the global scope:

    def metadata():
        global tracknumber, artist, title
        # all of your current code
    
0
votes

In your code , the scope of variables tracknumber,artist and title are local to the function. In order to access those variables, either declare them as global variables or return those variables.