Files in Python:
Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).
We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).
We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.
As a result, a file operation in Python is performed in the following order:
- Create a new file
- You can either read or write (perform the operations)
- Close the file.
open() Function in Python:
The open() function opens a file and returns a file object as a result.
Syntax:
open(file, mode)
Parameters
file: It is the path and the filename
mode: A string indicating the mode in which the file should be opened.
“r”:
Read - It is the default setting. Opens a file for reading; an error is returned if the file does not exist.
“a”:
Append. Opens a file for appending; if the file does not exist, it is created.
“w”:
Write. Opens a file for writing and creates it if it does not already exist.
“x”:
Create. Creates the given file and returns an error if the file already exists.
You can also specify whether the file must be handled in binary or text mode.
“t”:
Text - This is the default value. The text mode
“b”:
Binary mode is denoted by the letter "b." (for example images)
Python open() Function with Examples
Method #1: Read a File
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
- Read the given file using the read() function(get the contents) and print the result.
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read-only mode. In this case, we're simply reading the contents of the file. gvn_file = open(givenFilename, 'r') # Read the given file using the read() function(get the contents) and print the result print(gvn_file.read())
Output:
hello this is btechgeeks good morning btechgeeks
File Content:
hello this is btechgeeks good morning btechgeeks
Method #2: Read and Write a File
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in write mode. In this case, we’re writing the contents into the file.
- Write some random text into the given file using the write() function.
- Close the given file.
- Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
- Read the given file using the read() function(get the contents) and print the result.
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in write mode. In this case, we're writing the contents into the file. gvn_file = open(givenFilename, 'w') # Write some random text into the given file using the write() function gvn_file.write("hello this is btechgeeks") # Close the given file gvn_file.close() # Open the file in read-only mode. In this case, we're simply reading the contents of the file. gvn_file = open(givenFilename,'r') # Read the given file using the read() function(get the contents) and print the result print(gvn_file.read())
Output:
hello this is btechgeeks
File Content:
hello this is btechgeeks
Google Colab Images:
Files and Code: