Python File Handling Briefly With Examples |
Python File Handling Briefly With Examples
Python File Handling: Python File handling is a mechanism to store the data on the disk permanently.it is an output of a Python Program in a file and to perform various operations perform creating, reading, writing, updating, and deleting files.
Python Operation on File
1. Opening of file.
2. Writing into a file.
3. Appending data into a file.
4. Reading from a file.
5. Closing of file.
Important Topic Post file Handling
👉Union Data Types
File Opening Modes: A Python opening File can be opened in a different mode to perform read and write operations on a file.
the open() function can be used to open a file, the open function takes two arguments.
open(filename,open mode).
For example,, if you want to open the icoderweb.txt file in writing mode then you should write
f= open("icoderweb.txt","w")
For example,, if you want to open the icoderweb.txt file in writing mode then you should write
f= open("icoderweb.txt","w")
Python file opening mode is given below
1. w: Open a file for writing
Creates a new file if it does not exist or truncates the file if it exists.
2. a: Open a file for appending data at the end of the file.it does not truncate the file.it creates a new file if the file does not exist.
3. r: Open a file for reading.
4. t: Opens a file in text mode.
5. b: Opens a file in binary mode.
6. w+:Opens a file for both writing and reading.
7. r+:Opens a file for both reading and writing.
The file pointer is placed at the beginning of the file.
1. w: Open a file for writing
Creates a new file if it does not exist or truncates the file if it exists.
Creates a new file if it does not exist or truncates the file if it exists.
2. a: Open a file for appending data at the end of the file.it does not truncate the file.it creates a new file if the file does not exist.
3. r: Open a file for reading.
4. t: Opens a file in text mode.
5. b: Opens a file in binary mode.
6. w+:Opens a file for both writing and reading.
7. r+:Opens a file for both reading and writing.
The file pointer is placed at the beginning of the file.
The file pointer is placed at the beginning of the file.
How To Write File Operation in Python?
f= open("icoderweb.txt", "w"); f.write("Hello friends Welcome To Icoderweb Website")f.close();
*****OUTPUT*****Hello friends Welcome To Icoderweb Website
f= open("icoderweb.txt", "w");
f.write("Hello friends Welcome To Icoderweb Website")
f.close();
*****OUTPUT*****
Hello friends Welcome To Icoderweb Website
Python Appending data in the file: A Python file for appending data at the end of the file.it does not truncate the file.
How To Appending File Operation in Python?
f= open("icoderweb.txt", "a"); f.write(" You Can Learn Python Language")f.close();
*****OUTPUT*****Hello friends Welcome To Icoderweb Website You Can Learn Python Language
f= open("icoderweb.txt", "a");
f.write(" You Can Learn Python Language")
f.close();
*****OUTPUT*****
Hello friends Welcome To Icoderweb Website You Can Learn Python Language
Python Reading file: A Python Reading all the contents of the file.read() function is used to read the content of a file.
Syntax of read() function:read(counter).
Here counter is the number of bytes to be read from the file starting from the beginning of the file.
the counter is optional.
Syntax of read() function:read(counter).
Here counter is the number of bytes to be read from the file starting from the beginning of the file.
the counter is optional.
How To Reading File Operation in Python?
f= open("icoderweb.txt", "r"); fdata=f.read()print("File Data:",fdata)f.close();
*****OUTPUT*****File Data: Hello friends Welcome To Icoderweb Website You Can Learn Python Language
f= open("icoderweb.txt", "r");
fdata=f.read()
print("File Data:",fdata)
f.close();
*****OUTPUT*****
File Data: Hello friends Welcome To Icoderweb Website You Can Learn Python Language
How To Reading File 20 Byte Operation in Python?
f= open("icoderweb.txt", "r"); fdata=f.read(20)print("20 Byte File Data:",fdata)f.close();
*****OUTPUT*****20 Byte File Data: Hello friends Welcome
f= open("icoderweb.txt", "r");
fdata=f.read(20)
print("20 Byte File Data:",fdata)
f.close();
*****OUTPUT*****
20 Byte File Data: Hello friends Welcome
Python Reading lines file: A Python readline() function is used to read a file line by line from the beginning of the file.
How To Readline File Operation in Python?
f= open("icoderweb.txt", "r"); rline=f.readline()print("Reading Data:",rline)f.close();
*****OUTPUT*****Reading Data: Hello friends Welcome To Icoderweb Website
f= open("icoderweb.txt", "r");
rline=f.readline()
print("Reading Data:",rline)
f.close();
*****OUTPUT*****
Reading Data: Hello friends Welcome To Icoderweb Website
Python Reading line by line file: A Python readline() function is used to read a file line by line from the beginning of the file. If we want to read two lines of the file then just use readline() method two times.
How To Readline File Operation in Python?
f= open("icoderweb.txt", "r"); rline1=f.readline()rline2=f.readline()print("Reading Data rline1:",rline1)print("Reading Data rline1:",rline2)f.close();
*****OUTPUT*****Reading Data rline1:Hello friends Welcome To Icoderweb Website
Reading Data rline2:You Can Learn Python Language
f= open("icoderweb.txt", "r");
rline1=f.readline()
rline2=f.readline()
print("Reading Data rline1:",rline1)
print("Reading Data rline1:",rline2)
f.close();
*****OUTPUT*****
Reading Data rline1:Hello friends Welcome To Icoderweb Website
Reading Data rline2:You Can Learn Python Language
How To Reading Character File Operation in Python?
Let us suppose data:icoderweb@gmail.com
f= open("icoderweb.txt", "r"); fcontent=f.read()count=0for i in fcontent: count=count+1;
print("Total Character:",count)f.close();
*****OUTPUT*****Total Character:18
Let us suppose data:icoderweb@gmail.com
f= open("icoderweb.txt", "r");
fcontent=f.read()
count=0
for i in fcontent:
count=count+1;
print("Total Character:",count)
f.close();
*****OUTPUT*****
Total Character:18
How To Reading Alphabet File Operation in Python?
Let us suppose data:icoderweb8077@gmail.com
f= open("icoderweb.txt", "r"); fcontent=f.read()count=0for i in fcontent: if I.idalpha(): count=count+1;
print("Total Alphabet:",count)f.close();
*****OUTPUT*****Total Alphabet:17
Let us suppose data:icoderweb8077@gmail.com
f= open("icoderweb.txt", "r");
fcontent=f.read()
count=0
for i in fcontent:
if I.idalpha():
count=count+1;
print("Total Alphabet:",count)
f.close();
*****OUTPUT*****
Total Alphabet:17
How To Reading digit File Operation in Python?
Let us suppose data:icoderweb8077@gmail.com
f= open("icoderweb.txt", "r"); fcontent=f.read()count=0for i in fcontent: if i.isdigit(): count=count+1;
print("Total Digit:",count)f.close();
*****OUTPUT*****Total Alphabet:4
Let us suppose data:icoderweb8077@gmail.com
f= open("icoderweb.txt", "r");
fcontent=f.read()
count=0
for i in fcontent:
if i.isdigit():
count=count+1;
print("Total Digit:",count)
f.close();
*****OUTPUT*****
Total Alphabet:4