0% found this document useful (0 votes)
3 views11 pages

File Handling Techniques in Python

The document contains various Python code snippets demonstrating file operations such as reading, writing, appending, and manipulating file content. It includes examples of different file modes like 'r+', 'w+', and 'a+', along with tasks such as counting words, letters, and digits, and converting digits to English words. Additionally, it shows how to separate odd and even numbers into different files and remove newline characters from a file.

Uploaded by

expertcoding63
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

File Handling Techniques in Python

The document contains various Python code snippets demonstrating file operations such as reading, writing, appending, and manipulating file content. It includes examples of different file modes like 'r+', 'w+', and 'a+', along with tasks such as counting words, letters, and digits, and converting digits to English words. Additionally, it shows how to separate odd and even numbers into different files and remove newline characters from a file.

Uploaded by

expertcoding63
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1️⃣ Open a file and read entire content

file = open("[Link]", "r")


content = [Link]()
print(content)
[Link]()

2️⃣ Open a file and write content


file = open("[Link]", "w")
[Link]("how are you?")
[Link]()

3️⃣ Open a file and append data


file = open("[Link]", "a")
[Link](" hiiii")
[Link]()

4️⃣ W+ mode example (write + read)


file_name = "[Link]"

with open(file_name, "w+") as file:


[Link]("Hello, this is a test using w+ mode.\n")
[Link]("This mode allows both writing and reading.\n")
[Link](0)
content = [Link]()
print(content)

5️⃣ r+ mode example


file_name = "[Link]"

with open(file_name, "w") as file:


[Link]("This is the original content.\n")

with open(file_name, "r+") as file:


print([Link]())
[Link](0)
[Link]("Updated content.\n")
[Link](0)
print([Link]())

6️⃣ a+ mode example


file_name = "[Link]"

with open(file_name, "a+") as file:


[Link]("This is\n")
[Link](0)
print([Link]())

7️⃣ Read file using read()


file = open("[Link]", "r")
content = [Link]()
print(content)
[Link]()

8️⃣ Read file using readline()


file = open("[Link]", "r")
line1 = [Link]()
line2 = [Link]()
print(line1)
print(line2)
[Link]()
9️⃣ Read file using readlines()
file = open("[Link]", "r")
lines = [Link]()
print(lines)
[Link]()

🔟 Read a file line by line and store into a list


file = open("[Link]", "r")
lines = []
for line in file:
[Link](line)
print(lines)
[Link]()

1️⃣1️⃣ Read a file line by line and store into a variable


file = open("[Link]", "r")
content = ""
for line in file:
content += line
print(content)
[Link]()

1️⃣2️⃣ Read first n lines of a file


file = open("[Link]", "r")
n=3
for i in range(n):
print([Link]())
[Link]()

1️⃣3️⃣ Read last n lines of a file


file = open("[Link]", "r")
lines = [Link]()
n=2
for line in lines[-n:]:
print(line)
[Link]()

1️⃣4️⃣ Print words containing digits only (AKTU PYQ)


file = open("[Link]", "r")
words = [Link]().split()

for word in words:


if [Link]():
print(word)

[Link]()

1️⃣5️⃣ Convert digits into English words


words = input().split()

numbers = {
'0': 'zero', '1': 'one', '2': 'two',
'3': 'three', '4': 'four', '5': 'five',
'6': 'six', '7': 'seven', '8': 'eight',
'9': 'nine'
}

for word in words:


if [Link]():
print(numbers[word], end=" ")
else:
print([Link](), end=" ")

1️⃣6️⃣ Separate odd and even numbers into files


file = open("[Link]", "r")
nums = [Link]().split()
[Link]()

odd = open("[Link]", "w")


even = open("[Link]", "w")

for n in nums:
if int(n) % 2 == 0:
[Link](n + " ")
else:
[Link](n + " ")

[Link]()
[Link]()
1️⃣7️⃣ Capitalize first letter of every word in a file
file = open("[Link]", "r")
content = [Link]()
[Link]()

file = open("[Link]", "w")


[Link]([Link]())
[Link]()

1️⃣8️⃣ Count frequency of words in a file


file = open("[Link]", "r")
words = [Link]().split()
[Link]()

freq = {}

for word in words:


freq[word] = [Link](word, 0) + 1
print(freq)

1️⃣9️⃣ Copy content of one file to another


src = open("[Link]", "r")
dest = open("[Link]", "w")

[Link]([Link]())

[Link]()
[Link]()

2️⃣0️⃣ Count letters and digits in a file


file = open("[Link]", "r")
content = [Link]()
[Link]()

letters = digits = 0

for ch in content:
if [Link]():
letters += 1
elif [Link]():
digits += 1

print("Letters:", letters)
print("Digits:", digits)

2️⃣1️⃣ Count number of lines, words and characters


file = open("[Link]", "r")
lines = [Link]()
[Link]()

line_count = len(lines)
word_count = sum(len([Link]()) for line in lines)
char_count = sum(len(line) for line in lines)

print(line_count, word_count, char_count)

2️⃣2️⃣ Remove newline characters from file


file = open("[Link]", "r")
lines = [Link]()
[Link]()

file = open("[Link]", "w")


for line in lines:
[Link]([Link]())
[Link]()

2️⃣3️⃣ Use of seek()


file = open("[Link]", "r")
[Link](0)
print([Link]())
[Link]()

You might also like