在Python中找到丢失的封面艺术

时间:2015-04-13 23:54:28

标签: python recursion

所以我想要做的就是遍历目录,对于包含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))

2 个答案:

答案 0 :(得分:1)

您需要致电os.listdir

上的os.getcwd()
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', ...]

答案 1 :(得分:0)

除了A.J.关于使用os.listdir,您还检查了folder.jpg的错误文件夹(os.getcwd()没有更改,使用相同的名称列表和功能,并重复检查每个& #34; * .flac" file。

相反,你应该尝试:

def noart():
    result = []
    for root, dirnames, filenames in os.walk(pwd):
        if any(fn.endswith(".flac") for fn in filenames):
            # Check once if any of the files match *.flac
            if "folder.jpg" not in filenames:
                # Check for art
                result.append(root)
    return result