Skip to content Skip to sidebar Skip to footer

Make Empty File For Each Subfolder Using Subfolders' Name In Python

If I have a folder structure as follows: folder \ sub1\sub1_1 \ sub1\sub1_2 \ sub1\sub1_3 . . . \ sub2\sub2_1 \ sub2

Solution 1:

You can do that recursively:

from os import listdir
from os.path import isfile, join

mypath = "add/your/path"

def write_files(path):
    folders = [f for f in listdir(path) ifnot isfile(join(path, f))]
    iflen(folders) == 0:
        #Writing the actual File
        open(path+"/"+path.split("/")[-1]+".xlsx", "w+")
    else:
        for folder in folders:
            write_files(path+"/"+folder)

write_files(mypath)

Note that this will only create textfiles with an xlsx ending and not actual excel files. Therefore you can install some library that is capable of creating excel files

Post a Comment for "Make Empty File For Each Subfolder Using Subfolders' Name In Python"