File creation is extremely easy with Python: you simply create the variable that will represent the file, open the file, give it a filename, and tell Python that you want to write to it.
If you don't expressly tell Python that you want to write to a file, it will be opened in read-only mode. This acts as a safety feature to prevent you from accidentally overwriting files. In addition to the standard w to indicate writing and r for reading, Python supports several other file access modes:
- a: Appends all output to the end of the file; it does not overwrite information currently present. If the indicated file does not exist, it is created.
- r: Opens a file for input (reading). If the file does not exist, an IOError exception is raised.
- r+: Opens a file for input and output. If the file does not exist, causes an IOError exception.
- w: Opens a file for output (writing...