A simple Python program that reads a textfile and creates 2-level folder structure based on the contents of the textfile


import os, sys

# If directory (path) doesn’t exist, create it
def createPath(path):
   if not os.path.isdir(path):
      os.mkdir(path)

f = open(r"F:\Folders.txt")

folder = "F:\\Documents\\"

## Read the first line
line = f.readline()

## If the file is not empty keep reading line one at a time till the file is empty

i=1
while line:
    #print(line)
    for char in '\n?.!/;:':
       line = line.replace(char,'')
    position = line.find("*")
    if position == 0:
      #print("line is {}" .format(line[1:]))
      folder2 = folder + str(i) + "." + line[1:]
      print(folder2)
      createPath(folder2)
      j=1
      i=i+1
    else:
      if len(line.strip()) != 0:
        folder3 = folder2 + "\\" + str(j) + "." + line
        print(folder3)
        createPath(folder3)
        j=j+1
    line = f.readline()
f.close()

Comments

Popular posts from this blog

How to create an organizational chart in your webpage using Google Organization Chart Tools