IndentationError in python script for organising Music
Question: I have made this script in the past and I want to use it now, but an error
occurs when trying to run it. This script is about organising my music. I have
a directory organised by label and want to grab the artist name from the
directory names inside label and year directory and create new directories
inside the artist and year directory.

The names of directories inside label are like this

LabelName_[Artist-AlbumName]_2015-08-09

And want to create symlinks inside artist directories and year directories( by
date ) like this

2015-08-09_[Artist-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
    

What is going wrong? Is part.replace outdated or something? Any suggestion to
improve this script would be appreciated.


Answer: You are mixing tabs and spaces; taking the source from your post here shows:

    
    
    >>> '''\
    ...             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("[","")']
    

Note the `\t` at the start of the `if` line. Python expands tabs to _8 spaces_
, but you probably have your editor set to use 4 spaces instead. So Python
sees this:

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

where your editor shows you this:

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

Configure your editor to use _spaces only_ for indentation. A good editor will
convert `TAB` keys into spaces for you if so configured.

Quoting from the [Python Style
Guide](http://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) (PEP 8):

> Spaces are the preferred indentation method.
>
> Tabs should be used solely to remain consistent with code that is already
> indented with tabs.

