Skip to main content
 首页 » 编程设计

python之在 Python 中查找丢失的封面艺术

2025年05月04日12pengyingh

所以我想做的是遍历目录,对于包含 flac 文件的每个文件夹,检查它是否也包含“folder.jpg”文件。如果没有,则将目录打印到终端。告诉我哪里出错了。

noart = [] 
def noart(): 
#pwd is defined 
  for root, dirnames, filenames in os.walk(pwd): 
    for filename in fnmatch.filter(filenames, "*.flac"): 
      if "folder.jpg" not in os.getcwd(): 
        noart.append(os.path.join(root, filename)) 

请您参考如下方法:

您需要在 os.getcwd() 上调用 os.listdir:

noart = [] 
def noart(): 
#pwd is defined 
  for root, dirnames, filenames in os.walk(pwd): 
    for filename in fnmatch.filter(filenames, "*.flac"): 
      if "folder.jpg" not in os.listdir(os.getcwd()): 
        noart.append(os.path.join(root, filename)) 

os.getcwd() 只返回当前工作目录的路径:

>>> os.getcwd() 
'/Users/aj' 
>>> os.listdir(os.getcwd()) 
['Desktop', 'Documents', ...]