How do I delete a file? (And other file questions...)
Use the os.remove function:
You sometimes see os.unlink used instead. The two functions are identical; unlink() is simply the name of the Unix system call used by this function.
To remove a directory, use os.rmdir; use os.mkdir to create one. os.makedirs will create any intermediate directories in path that don’t exist. os.removedirs will remove intermediate directories as long as they’re empty; if you want to delete an entire directory tree and its contents, use shutil.rmtree.
To rename a file, use os.rename.
To truncate a file, open it using for updating, and use the file.truncate method to cut it off:
f = open(filename, "r+") f.truncate(size)