Bakira
Bakira

Reputation: 111

Python: Walk through directory and save all foldernames, subfolder and files in a csv-file

I have a directory full of music. Every folder corresponds to one interpret and contains folders for each album. In these album-folder are all titles saved. So we get this structure:

and so on.

Now I want to walk through this directory, read out all folder names and save them in a csv file as follow:

At the end, I can open the csv file in excel and get a well structed database of my music folder.

What I did by now:

import csv
import os

PATH = "D:\Desktop\MusikAlt"
INTERPRETEN = []
ALBEN = []
TITEL = []


for Interpret, Album, Titel in os.walk(PATH):
    print('The current folder is ' + Interpret)
    INTERPRETEN.append(Interpret)

    for album in Album:
        print('Albums from the interpret: ' + Interpret + ': ' + album)
        ALBEN.append(album)

    for titel in Titel:
        print(titel)
        TITEL.append(titel)

    print('')

I'm stuggling right now, modifying the list to the needed structure and pass them to a csv file. Any idea?

Greatly appreciated, Thanks!

Upvotes: 0

Views: 1331

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177556

Given an input directory:

C:\DEMO
├───Interpret A
│   ├───Album A
│   │       Title 01
│   │       Title 02
│   │
│   └───Album B
│           Title 03
│           Title 04
│
└───Interpret B
    ├───Album C
    │       Title 05
    │       Title 06
    │
    └───Album D
            Title 07
            Title 08

This code will do what you want. In case you have non-ASCII characters in your music titles, encoding='utf-8-sig' ensures the data will be read and displayed in Excel properly. newline='' is a requirement from the csv documentation and will prevent Excel from double-spacing the data rows.

import csv
import os

PATH = r"C:.\demo"

with open('data.csv','w',newline='',encoding='utf-8-sig') as f:
    w = csv.writer(f)

    # Write a header row
    w.writerow('Interpret Album Title'.split())

    # path is the current directory being walked.
    # dirs is a list of the directories in this path.  It can be edited to skip directories.
    # files is a list of files in this path.
    for path,dirs,files in os.walk(PATH):
        for file in files:
            # Join the path to the current file.
            current = os.path.join(path,file)
            # Remove the root path.
            # Split the remaining "Interpret\Album\Title" into a list.
            row = os.path.relpath(current,PATH).split(os.sep)
            w.writerow(row)

data.csv:

Interpret,Album,Title
Interpret A,Album A,Title 01
Interpret A,Album A,Title 02
Interpret A,Album B,Title 03
Interpret A,Album B,Title 04
Interpret B,Album C,Title 05
Interpret B,Album C,Title 06
Interpret B,Album D,Title 07
Interpret B,Album D,Title 08

Upvotes: 1

Related Questions