5,337
edits
Line 41: | Line 41: | ||
with open('your_file.txt', 'w') as f: | with open('your_file.txt', 'w') as f: | ||
f.write("\n".join(my_list)) | f.write("\n".join(my_list)) | ||
</syntaxhighlight> | |||
====Create Directory if it doesn't exist==== | |||
[https://stackoverflow.com/questions/12517451/automatically-creating-directories-with-file-output Reference] | |||
<syntaxhighlight lang="python"> | |||
def ensure_dir_exists(filename): | |||
if not os.path.exists(os.path.dirname(filename)): | |||
try: | |||
os.makedirs(os.path.dirname(filename)) | |||
except OSError as exc: # Guard against race condition | |||
if exc.errno != errno.EEXIST: | |||
raise | |||
</syntaxhighlight> | </syntaxhighlight> | ||