我在一个目录中有一组文件,我正在使用 python-magic 库来过滤掉“text/plain”类型的文件并删除所有非“text/plain”文件。下面是我正在使用的代码
import os
import magic
def ftype(path):
fpath = path
mime = magic.Magic(mime=True)
for root, dirs, fnames in os.walk(path):
for fname in fnames:
mi = mime.from_file(fpath+'\\'+fname)
if not mi.endswith('plain'):
os.remove(fpath + '\\' + fname)
print(fname)
else:
pass
ftype('filepath')
我能够在一小部分文件上成功运行脚本。但是,当我在包含大约 40000 个文件的目录上运行脚本时,出现以下错误。
Traceback (most recent call last):
File "C:\Users\dmg\AppData\Local\Programs\Python\Python37\lib\site-packages\magic\magic.py", line 91, in from_file
return self._handle509Bug(e)
File "C:\Users\dmg\AppData\Local\Programs\Python\Python37\lib\site-packages\magic\magic.py", line 100, in _handle509Bug
raise e
File "C:\Users\dmg\AppData\Local\Programs\Python\Python37\lib\site-packages\magic\magic.py", line 89, in from_file
return maybe_decode(magic_file(self.cookie, filename))
File "C:\Users\dmg\AppData\Local\Programs\Python\Python37\lib\site-packages\magic\magic.py", line 255, in magic_file
return _magic_file(cookie, coerce_filename(filename))
File "C:\Users\dmg\AppData\Local\Programs\Python\Python37\lib\site-packages\magic\magic.py", line 196, in errorcheck_null
raise MagicException(err)
magic.magic.MagicException: b"line I64u: regex error 14 for `^[[:space:]]*class[[:space:]]+[[:digit:][:alpha:]:_]+[[:space:]]*\\{(.*[\n]*)*\\}(;)?$', (failed to get memory)"
我不确定是什么问题。有人可以帮我解决这个问题,或者是否有任何替代方法可以进行上述操作。
更新:尝试以下评论中所述的一些方法后,问题仍然存在。