Python provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in Python, normal text files and binary files (written in binary language,
- Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (
‘\n’) in Python by default.
- Binary files: In this type of file, there is no terminator for a line and the data is stored after converting it into machine-understandable binary language.
Refer the below articles to get the idea about basics of file handling.
Opening a file
Opening a file refers to getting the file ready either for reading or for writing. This can be done using the
open() function. This function returns a file object and takes two arguments, one that accepts the file name and another that accepts the mode(Access Mode). Now, the question arises what is access mode?
Access modes govern the type of operations possible in the opened file. It refers to how the file will be used once it’s opened. These modes also define the location of the File Handle in the file. File handle is like a cursor, which defines from where the data has to be read or written in the file. There are 6 access modes in python.
- Read Only (‘r’): Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exist, raises I/O error. This is also the default mode in which the file is opened.
- Read and Write (‘r+’): Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exist.
- Write Only (‘w’): Open the file for writing. For existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
- Write and Read (‘w+’): Open the file for reading and writing. For existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
- Append Only (‘a’): Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
- Append and Read (‘a+’): Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
File_object = open(r"File_Name", "Access_Mode")
Note: The file should exist in the same directory as the Python script, otherwise full address of the file should be written.
Example #1: Suppose the text file looked like this
We want to read the content of the file using Python.
Welcome to GeeksForGeeks!!
Example #2: Suppose we want to write more data to the above file using Python.
- How to open and close a file in Python
- Python program to reverse the content of a file and store it in another file
- Create a GUI to convert CSV file into excel file using Python
- reStructuredText | .rst file to HTML file using Python for Documentations
- Python | os.open() method
- Python PIL | Image.open() method
- Open a new Window with a button in Python-Tkinter
- Python | sympy.sets.open() method
- Python script to open a Google Map location on clipboard
- Python - Get file id of windows file
- Reading Python File-Like Objects from C | Python
- Python append to a file
- File Objects in Python
- Writing to file in Python
- File Handling in Python
- Python | Kivy .kv File
- File Searching using Python
- How to read from a file in Python
- Python | PageLayout in Kivy using .kv file
- Extract text from PDF File using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.