python-file-handling.md

#Python File Open

File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files.


#File Handling

The key function for working with files in Python is the open() function. This function takes two parameters: filename and mode.

#Opening Modes

There are four different methods (modes) for opening a file:

#File Types

In addition, you can specify if the file should be handled as binary or text mode:


#Syntax

To open a file for reading, it is enough to specify the name of the file:

pythonf = open("demofile.txt")

The code above is the same as:

pythonf = open("demofile.txt", "rt")

Because "r" for read and "t" for text are the default values, you do not need to specify them.

Note: Make sure the file exists, or else you will get an error.