This script creates a hierarchy of directories. Is this a good approach or can it be made better? I am mainly concerned with maintainability here, not efficiency. Suppose I wanted to add more directories to this hierarchy. Would this structure of the script make it easier to do?
Hierarchy
./project_euler
./001_050
./051_100
...
./401_450
./codechef
./easy
./medium
./hard
./spoj
./functions
./utilities
The script
import os
TOP = ['project_euler', 'codechef', 'spoj', 'functions',
'utilities']
CODECHEF = ['easy', 'medium', 'hard']
def safe_make_folder(i):
'''Makes a folder if not present'''
try:
os.mkdir(i)
except:
pass
def make_top_level(top):
for i in top:
safe_make_folder(i)
def make_euler_folders(highest):
def folder_names():
'''Generates strings of the format 001_050 with
the 2nd number given by the function argument'''
for i in range(1,highest, 50):
yield (
'project_euler' + os.sep +
str(i).zfill(3) + '_' + str(i + 49).zfill(3)
)
for i in folder_names():
safe_make_folder(i)
def make_codechef_folders(codechef):
for i in codechef:
safe_make_folder('codechef' + os.sep + i)
def main():
make_top_level(TOP)
make_euler_folders(450)
make_codechef_folders(CODECHEF)
if __name__ == "__main__":
main()
Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.
\$\endgroup\$ – Anthon Jul 20 '13 at 16:37