File Handling in Python
File Handling in Python
Explanation:
Key Differences:
w+ mode:
Opens a file for reading and writing.
Deletes existing content if the file exists or creates a new file if it doesn't.
r+ mode:
Opens a file for reading and writing.
Requires the file to already exist; does not delete existing content.
Example:
# w+ mode
with open('wplus_example.txt', 'w+') as file:
file.write("Using w+ mode!")
file.seek(0)
print(file.read()) # Output: Using w+ mode!
# r+ mode
with open('rplus_example.txt', 'r+') as file:
file.write("Using r+ mode!")
file.seek(0)
print(file.read()) # Output: Using r+ mode!
Explanation:
Explanation:
The with statement ensures the file is automatically closed after operations,
even if an error occurs.
Explanation:
Explanation:
def file_statistics(file_name):
with open(file_name, 'r') as file:
lines = file.readlines()
num_lines = len(lines)
num_words = sum(len(line.split()) for line in lines)
num_chars = sum(len(line) for line in lines)
return num_lines, num_words, num_chars
Explanation:
Explanation:
search_word('example.txt', 'Python')
Explanation:
Checks each line to see if the word is present using if word in line .
def remove_blank_lines(file_name):
with open(file_name, 'r') as file:
lines = file.readlines()
remove_blank_lines('example.txt')
Explanation:
def word_frequency(file_name):
with open(file_name, 'r') as file:
content = file.read().split()
return {word: content.count(word) for word in set(content)}
print(word_frequency('example.txt'))
def reverse_file_content(file_name):
with open(file_name, 'r') as file:
content = file.read()
with open(file_name, 'w') as file:
file.write(content[ :-1])
reverse_file_content('example.txt')
Explanation:
Explanation:
content.replace(old_word, new_word) : Replaces all occurrences of the old
word with the new word.
Explanation:
Opens three files simultaneously and writes content from the first two files into
the third.
import os
def file_size(file_name):
return os.path.getsize(file_name)
Explanation:
Explanation:
def count_vowels_consonants(file_name):
vowels = 'aeiouAEIOU'
with open(file_name, 'r') as file:
content = file.read()
num_vowels = sum(1 for char in content if char in vowels)
num_consonants = sum(1 for char in content if char.isalpha() a
return num_vowels, num_consonants
print(count_vowels_consonants('example.txt'))
Explanation:
backup_file('example.txt', 'example_backup.txt')
Explanation:
print(find_word_position('example.txt', 'Python'))
Explanation:
split_file('large_file.txt', 1024)
Explanation:
Reads and writes the file content in chunks, each of a specified size, creating
multiple smaller files.
try:
file = open('example.txt', 'r')
print("File opened successfully!")
except FileNotFoundError:
print("File not found!")
Explanation:
The close() method closes the file and releases associated system resources.
Ensures that buffered data is written to the file.
Example:
Use the 'a' mode to append data to the end of a file without overwriting existing
content.
26. Reading a Non-Existent File
If you attempt to read a file that does not exist, Python raises a
FileNotFoundError .
Example:
a mode:
Opens a file for appending.
Writing starts at the end of the file; reading is not allowed.
a+ mode:
Allows both reading and appending to the file.
Example: