I am a new python programmer and have been making a file sort function to take a file name and neatly arrange it in a file structure being year/month/day. The following code works but looks ugly and there is a lot of duplicate exception errors which I would like to remove. Would love to see how to improve the efficiency of this code as it will be run frequently. Thanks in advance
def fileSort(day, month, year, file):
global filewritten
try: os.makedirs(togoto + '/' + year)
except FileExistsError:
pass
try: os.makedirs(togoto + '/' + year + '/' + month)
except FileExistsError:
pass
try:
os.makedirs(togoto + '/' + year + '/' + month + '/' + day)
except FileExistsError:
pass
try:
shutil.move(path + '/' + file,
togoto + '/' + year + '/' + month + '/' + day + '/' + file)
filewritten += 1
except FileExistsError:
pass