我以前编写过这个脚本,我现在想使用它,但是在尝试运行它时会发生错误。这个脚本是关于组织我的音乐。我有一个按标签组织的目录,想要从label和year目录中的目录名称中获取艺术家名称,并在artist和year目录中创建新目录。
标签内的目录名称是这样的
LabelName_ [艺术家-ALBUMNAME] _2015-08-09
并希望在艺术家目录和年份目录(按日期)内创建符号链接,如此
2015-08-09_ [艺术家-ALBUMNAME] _LabelName
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过时还是什么? 任何改进此脚本的建议都将受到赞赏。
答案 0 :(得分:3)
你正在混合标签和空格;从你的帖子中获取来源显示:
>>> '''\
... 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("[","")']
请注意\t
行开头的if
。 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):
空格是首选的缩进方法。
选项卡应仅用于与已使用选项卡缩进的代码保持一致。
答案 1 :(得分:2)
您可能正在混合空格和制表符,这使得很难弄清楚Python如何看待缩进。