python 用于组织音乐的脚本中的 IndentationError

IndentationError in python script for organising Music

我以前制作过这个脚本,现在想使用它,但是在尝试 运行 时出现错误。这个脚本是关于组织我的音乐的。我有一个按标签组织的目录,想从标签和年份目录中的目录名称中获取艺术家姓名,并在艺术家和年份目录中创建新目录。

label里面的目录名是这样的

标签名称_[艺术家-专辑名称]_2015-08-09

并且想像这样在艺术家目录和年份目录(按日期)内创建符号链接

2015-08-09_[艺术家-专辑名称]_标签名称

import os
basedir = "/home/zab/Music/#01.Label"
artist_parent_dir = "/home/zab/Music/#03.Artist"
date_parent_dir = "/home/zab/Music/#04.ReleaseDate"
for fn in os.listdir(basedir):
    label_path = os.path.join( basedir, fn)
    for album in os.listdir(label_path):
        i = 1
        words = album.split("_")
        for word in words:
            if i == 1:
                label = word
            elif i == 2:
                name = word
            else:
                date = word
            i = i + 1
        artist_album = name.split("-")
        j = 1
        for part in artist_album:
            if j == 1:
                artist = part.replace("[","")
            j = j + 1
        date_parts = date.split("-")
        z =  1
        for part_two in date_parts:
            if z == 1:
                year = part_two
            z = z + 1
        if not os.path.isdir(os.path.join(artist_parent_dir,artist)):
            os.mkdir(os.path.join(artist_parent_dir,artist))
        if not os.path.isdir(os.path.join(date_parent_dir,year)):
            os.mkdir(os.path.join(date_parent_dir,year))
        src = os.path.join(label_path,album)
        artist_dst = os.path.join(artist_parent_dir, artist, name + "_" + label + "_" + date)
        year_dst = os.path.join(date_parent_dir,year, date + "_" + name + "_" + label)
        if not os.path.exists(artist_dst):
            os.symlink(src, artist_dst)
        if not os.path.exists(year_dst):
            os.symlink(src, year_dst)

File "/home/zab/Music/_Scripts/OrganizeByArtist.py", line 22
    artist = part.replace("[","")
                                ^
IndentationError: expected an indented block

出了什么问题? part.replace 过时了吗? 任何改进此脚本的建议将不胜感激。

您可能混用了空格和制表符,这使得很难弄清楚 Python 如何看待缩进。

您正在混用制表符和空格;从您的 post 此处获取源代码显示:

>>> '''\
...             for part in artist_album:
...                 if j == 1:
...                     artist = part.replace("[","")
... '''.splitlines()
['            for part in artist_album:', '\t            if j == 1:', '                    artist = part.replace("[","")']
>>> from pprint import pprint
>>> pprint(_)
['            for part in artist_album:',
 '\t            if j == 1:',
 '                    artist = part.replace("[","")']

注意 if 行开头的 \t。 Python 将制表符扩展为 8 个空格,但您可能将编辑器设置为使用 4 个空格。所以 Python 看到了这个:

for part in artist_album:
        if j == 1:
        artist = part.replace("[","")']

您的编辑向您显示的位置:

for part in artist_album:
    if j == 1:
        artist = part.replace("[","")']

将您的编辑器配置为仅使用 空格 进行缩进。如果这样配置,好的编辑器会为您将 TAB 键转换为空格。

引自 Python Style Guide (PEP 8):

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.