Python:File
lastmod 2015-10-30created 2015-03-245 edits
Automatically creating directories
To create the parent directory when it does not exist, do the following.
filename = "/foo/bar/baz.txt"
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
Example
-
os.path.isfile(fname) - Checks whether the file exists.
-
os.remove(filename) - Removes the file.
Troubleshooting
File write EOL
When using write(), you may find that the EOL is changed to CRLF or LR depending on the OS environment. In this case, adding wb to write's mode argument applies the EOL exactly as written.
Favorite site
Pages that link here