1

who can help me with the script?

If the audiofile.tag.genre is empty, i would like to enter the genre via input,

and every time when the audiofile.tag.genre is empty I want to accept the first genre from enter input

import eyed3
import eyed3.mp3
import sys, os, subprocess
import os, datetime

def get4():
    global genre

    if not genre:   
        while (True):
            try:
                genre = input("Input Genre: ")
            except ValueError:
                break
            break
            return genre
    else:
        return genre

topath = r"c:\dn"

def get(topath):
    os.chdir(topath)
    for filename in os.listdir(topath):
        if filename.endswith(".mp3"):
            audiofile = eyed3.load(filename)
            if not audiofile.tag.genre:
                get4()
                audiofile.tag.genre = genre
                audiofile.tag.save()

get(topath)

error:

 name 'genre' is not defined
kuck
  • 97
  • 6
  • `get` is not defined. As well `sys`, `subprocess`, and `datetime` are unused. You need to provide a [mre]. – wjandrea May 16 '20 at 19:43
  • edit: def get(topath) its correct – kuck May 16 '20 at 19:46
  • OK, now `genre` is not defined, because you haven't defined it. What were you expecting to happen? And why do you make it a global *and* return it? – wjandrea May 16 '20 at 19:49
  • Why does your `while` loop have an unconditional `break`? And why do you have `return genre` in the loop when you `break` right before it? – wjandrea May 16 '20 at 19:52
  • Are you trying to [ask the user for input until they give a valid response](https://stackoverflow.com/q/23294658/4518341)? – wjandrea May 16 '20 at 19:53
  • i forgot 2 lines, i need the genre, in def get (topath) – kuck May 16 '20 at 19:55
  • I've tried so much, I don't know how it should look that it works – kuck May 16 '20 at 19:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214022/discussion-between-kuck-and-wjandrea). – kuck May 16 '20 at 19:58

1 Answers1

0

As we discussed in chat, you want to get user input only if the mp3 doesn't have a genre already, but then use that same input for all the following mp3's. You can do that by setting a default, genre = None, then checking genre is None when needed.

Also I would use a more descriptive name than get4, like get_genre.

def get(topath):
    genre = None  # Default before user input
    ...
    if not audiofile.tag.genre:
        if genre is None:
            genre = get_genre()
        audiofile.tag.genre = genre
        audiofile.tag.save()

This way you don't use a global (which is messy), and you can simplify get_genre down to just asking the user for the genre then returning it.

wjandrea
  • 28,235
  • 9
  • 60
  • 81