Search code examples
pythontypeerrorid3musicbrainzmutagen

Trying to create a ID3-Tag editor. TypeError: Missing filename or fileobj argument


I am creating a tageditor that displays the ID3-Tags of an mp3 file in a "before" "after" style in different textLines. If there's no tag available, nothing is displayed. You are also able to edit the "after" textLines and any changes made to them should be saved to the file but when I press button2 I am getting the bottom traceback. How can I save the lines6-10 as the new "audio["title"], audio["artist"]" etc? This is the GUI

import sys
import easygui
import mutagen
from mutagen.easyid3 import EasyID3
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QLineEdit
lied = None
error = "No ID3-Tag available."
class TrackTag1(QDialog):
    def __init__(self):
        super(TrackTag1,self).__init__()
        loadUi("TrackTag1.ui",self)
        self.setWindowTitle("TrackTag")

@pyqtSlot()
def on_button1_clicked(self):
    #root.fileName = filedialog.askopenfilename( filetypes = ( ("Musik Dateien", "*.mp3"), ("Alle Dateien", "*.*") ) )
    #print(easygui.fileopenbox('MP3 Dateien','', '*.MP3'))
    lied = easygui.fileopenbox('MP3 Dateien','', '*.MP3')
    audio = EasyID3(lied)
    self.line0.setText(lied)                    #printing filepath to line0
    try:
        self.line1.setText(str(audio["title"]).strip("[']"))        #printing the ID3 tags after they've been stripped of "['']"
        self.line6.setText(str(audio["title"]).strip("[']"))
    except:
        KeyError
        self.line1.setText(error)
    try:
        self.line2.setText(str(audio["album"]).strip("[']"))
        self.line7.setText(str(audio["album"]).strip("[']"))
    except:
        KeyError
        self.line2.setText(error)
    try:
        self.line3.setText(str(audio["date"]).strip("[']"))
        self.line8.setText(str(audio["date"]).strip("[']"))
    except:
        KeyError
        self.line3.setText(error)
    try:
        self.line4.setText(str(audio["artist"]).strip("[']"))
        self.line9.setText(str(audio["artist"]).strip("[']"))
    except:
        KeyError
        self.line4.setText(error)
    try:
        self.line5.setText(str(audio["genre"]).strip("[']"))
        self.line10.setText(str(audio["genre"]).strip("[']"))
    except:
        KeyError
        self.line5.setText(error)

def on_button2_clicked(self):
    audio = EasyID3(lied)
    audio["title"] = self.line6.text()
    audio.save()



app=QApplication(sys.argv)
widget=TrackTag1()
widget.show()
sys.exit(app.exec_())




app=QApplication(sys.argv)
widget=TrackTag1()
widget.show()
sys.exit(app.exec_())

I'm getting this traceback when I press the save changes button:

Traceback (most recent call last):
  File "<string>", line 69, in on_button2_clicked
  File "h:\program files (x86)\python\lib\site-packages\mutagen\_util.py",     line 139, in wrapper
    writable, create) as h:
  File "h:\program files (x86)\python\lib\contextlib.py", line 59, in     __enter__
    return next(self.gen)
  File "h:\program files (x86)\python\lib\site-packages\mutagen\_util.py",     line 270, in _openfile
    raise TypeError("Missing filename or fileobj argument")
TypeError: Missing filename or fileobj argument

For now, you should only be able to edit the tags but I am planning to implement a MusicBrainz query soon.


Solution

  • In the method on_button2_clicked the lied object is basically None. To get the correct one, use the keyword global when assigning it in on_button1_clicked. (Which you actually never should! Instead make an attribute to store it and access it via self.lied or something similar)

    Also, I am assuming that the two functions are infact class methods owing to the self keyword and you simply got the indentation wrong while copy pasting.

    Basically an error due to scopes.