Answer the question
In order to leave comments, you need to log in
Why is the dictionary not being written to the JSON file?
The directorychooser function extracts tags from a single MP3 file located in the selected folder. These tags are written to a dictionary, which I planned to write to the JSON file. The code below completes without errors, but the JSON file remains empty. Couldn't find anything similar on google. Please, tell me what could be the problem?
import mutagen, datetime, pygame, os, json
from mutagen.id3 import ID3
from tkinter import *
from tkinter.filedialog import askdirectory
root = Tk()
root.minsize(300,300)
def directorychooser(event):
tags = {}
paths = []
directory = askdirectory()
os.chdir(directory)
for files in os.listdir(directory):
if files.endswith(".mp3"):
realdir = os.path.realpath(files)
paths.append(realdir)
path = paths[0]
audiofile = mutagen.File(path)
tags['Path to the File'] = path
song_title = audiofile.tags.getall('TIT2')
tags['Song Title'] = str(song_title[0])
album_title = audiofile.tags.getall('TALB')
tags['Album Title'] = str(album_title[0])
song_artist = audiofile.tags.getall('TPE1')
tags['Song Artist'] = str(song_artist[0])
album_artist = audiofile.tags.getall('TPE2')
tags['Album Artist'] = str(album_artist[0])
year_of_publishing = audiofile.tags.getall('TDRC')
tags['Year of Publishing'] = str(year_of_publishing[0])
track_number = audiofile.tags.getall('TRCK')
tags['Track Number'] = str(track_number[0])
tags['Length'] = str(datetime.timedelta(seconds = audiofile.info.length))
tags['Bitrate'] = audiofile.info.bitrate
with open("tags.json", "w") as t:
json.dump(tags, t)
choose_directory = Button(root,text = 'Choose Directory')
choose_directory.pack()
choose_directory.bind("<Button-1>", directorychooser)
root.mainloop()
Answer the question
In order to leave comments, you need to log in
You go to another directory:
Accordingly, the file is created there. (You can check)
Solution: Remember the directory of the call and before creating the file doos.chdir(exec_directory)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question