AttributeError: __enter__ when using with taglib.File(path)

68 views Asked by At

I was trying to write the following script, which has two functions - one for wiping all metadata of a m4a file and then set certain metadata using pytaglib, which is a Python binding for the C++ library.

import os
import subprocess
import taglib
import sys

def set_song_metadata_by_path(path, inviter, duetpartner):
   #with taglib.File(path, save_on_exit=True) as song:
   with taglib.File(path) as song:
       song.tags["ALBUM"] = inviter
       song.tags["ARTIST"] = inviter + duetpartner
       song.save()

# wipe metadata
def wipe_metadata(filetowipemetadata, output):
   if subprocess.call('ffmpeg -y -i "%s" -map_metadata -1 -c:v copy -c:a copy "%s"' % (filetowipemetadata, output), shell=True):
        sys.stderr.write("Error remuxing '%s', skipping." % (filetowipemetadata))
# here was continue, but no loop hence this

wipe_metadata('bored.m4a', 'bored2.m4a')
set_song_metadata_by_path('./bored2.m4a', 'test1', 'test2')

The error and stack trace I'm getting:

Traceback (most recent call last):
  File "/root/testbored.py", line 20, in <module>
    set_song_metadata_by_path('./bored2.m4a', 'test1', 'test2')
  File "/root/testbored.py", line 8, in set_song_metadata_by_path
    with taglib.File(path) as song:
AttributeError: __enter__

According to other answers I am overwriting my File() functionality it seems. But how do I fix that?

I am not using the save_on_exit=True, because it throws TypeError: __cinit__() got an unexpected keyword argument 'save_on_exit' hence I wrote song.save() at the end, it should work.

Update 1

This is the output of pip3 show pytaglib:

~# pip3 show pytaglib
Name: pytaglib
Version: 1.5.0
Summary: cross-platform, Python audio metadata ("tagging") library based on TagLib
Home-page: http://github.com/supermihi/pytaglib
Author: Michael Helmling
Author-email: [email protected]
License: GPLv3+
Location: /usr/local/lib/python3.9/dist-packages
Requires:
Required-by:

1

There are 1 answers

0
Sir Muffington On

Solved the problem by rewriting the code without with:

def set_song_metadata_by_path(path, inviter, duetpartner):
   #with taglib.File(path, save_on_exit=True) as song:
   #with taglib.File(path) as song:
   #    song.tags["ALBUM"] = inviter
   #    song.tags["ARTIST"] = inviter + duetpartner
   #    song.save()

   fileroni = taglib.File(path)
   fileroni.tags["ALBUM"] = inviter
   fileroni.tags["ARTIST"] = inviter + ' + ' + duetpartner
   fileroni.save()