ich habe mir eine Funktion geschrieben, um Text in eine .txt-Datei zu schreiben.
Zur Prüfung des Verzeichnisstrukturbaumes und der eventuellen Erstellung von
notwendigen Verzeichnisse habe ich folgenden Code geschrieben:
Code: Alles auswählen
def write_text_to_txt(self, output_file):
try: # if all necessary directories exist
f=open(output_file, 'w')
except IOError: # if directory error, create path
path=""
directory=re.split(r"/",output_file) #split path in folders
for i in range(len(directory)-1): # 0 to len(directory)-1
try: #create each part of the path, one by one
os.mkdir("{path}{directory}".format(path=path,directory=directory[i]))
path+=directory[i]
path+="/"
except OSError:
path+=directory[i]
path+="/"
f=open(output_file, 'w')
f.write(text)
f.close
Gibt es an dem Code Kritikpunkte?
Oder ist er zu umständlich?