# what do you mean by file handling in python programming
File handling in Python refers to the process of working with files on your
computer's storage system.
Python provides built-in functions and modules that allow you to create, read,
write, and manipulate files.
# Opening and Closing Files
You can open a file using the open() function.
It requires the file name and the mode in which you want to open the file (read,
write, append, etc.).
Once you're done working with the file, it's good practice to close it using the
close() method
# Opening a file in read mode
file = open(r'C:\Users\Ajit\Downloads\bank\save.txt', 'r')
file
Modes for Opening Files
'r': Read mode (default). Opens a file for reading.
'w': Write mode. Opens a file for writing. Creates a new file or truncates the
existing content.
'a': Append mode. Opens a file for writing at the end. Creates a new file if it
doesn't exist.
'b': Binary mode. Opens a file in binary mode (e.g., 'rb' for reading binary
files).
# how to read content of a file
content = file.read() # Reads the entire content of the file
print(content)
file.close()
# Writing to a File
file = open(r'C:\Users\Ajit\Downloads\bank\name.txt', 'w')
file.write('Hello, this is a sample text.')
file.close()
file=open(r'C:\Users\Ajit\Downloads\bank\ajit.txt')
file
content=file.read()
print(content)
# Opening a file in read mode
file = open(r'C:\Users\Ajit\Downloads\bank\save.txt', 'r')
content=file.read()
print(content)
file = open(r'C:\Users\Ajit\Downloads\bank\save.txt', 'a')
file.write('\n i am from sonipat.')
file = open(r'C:\Users\Ajit\Downloads\bank\save.txt', 'r')
content=file.read()
print(content)
file
print(content)
file
file = open(r'C:\Users\Ajit\Downloads\bank\save.txt', 'a')
file.write('\n i am from sonipat.')
file.close()
file
print(content)
# Using with Statement (Context Managers)
Python's with statement is used for automatically managing resources, such as
files. It ensures that files are properly closed after their suite finishes.
with open(r'C:\Users\Ajit\Downloads\bank\save.txt') as file:
content=file.read()
print(content)
# what is readline() method in file handling
The readline() method in file handling is used to read a single line from a file.
When you open a file in Python using the open() function and specify the mode as
'r' (read mode), you can use the readline() method to read individual lines within
that file.
# Opening a file in read mode using a raw string
file_path =r'C:\Users\Ajit\Downloads\bank\save.txt'
with open(file_path, 'r') as file:
line = file.readline() # Reads the first line
print(line)
# Opening a file in read mode using a raw string
file_path = r'C:\Users\Ajit\Downloads\bank\save.txt'
with open(file_path, 'r') as file:
line = file.readline() # Reads the first line
while line:
print(line.strip()) # The line.strip() function in Python is used to
remove
#any leading and trailing whitespace characters (such as spaces, tabs,
newline characters, etc.)
#from a string.
line = file.readline() # Reads the next line
file.readline() reads a single line from the file each time it's called.
line stores the content of the currently read line.
The while line: loop continues until the variable line is an empty string, which
happens when there are no more lines to read.
Inside the loop, print(line.strip()) displays the line content using print().
.strip() removes any leading or trailing whitespace characters (like spaces, tabs,
or newline characters) from the line before printing it.
line = file.readline() reads the next line, and the process continues until the end
of the file is reached.
# Writing to a File in readline()
file_path = r'C:\Users\Ajit\Downloads\bank\name.txt'
# Writing to a file
with open(file_path, 'w') as file:
file.write("This is line 1.\n")
file.write("This is line 2.\n")
file.write("This is line 3.\n")
file_path = r'C:\Users\Ajit\Downloads\bank\name.txt'
# Reading the entire file
with open(file_path, 'r') as file:
content = file.read()
print(content)
with open(r'C:\Users\Ajit\Downloads\bank\name.txt','r') as file:
line=file.readline()
print(file)
content=file.read()
print(content)
with open(r'C:\Users\Ajit\Downloads\bank\name.txt','r')as file:
line=file.readline()
while line:
print(line.strip())
line=file.readline()