0% found this document useful (0 votes)
4 views6 pages

Programs

The document provides a series of Python code snippets demonstrating various file operations, including creating, reading, writing, and manipulating text and binary files. It covers tasks such as counting characters, words, and lines, searching for specific words, and handling CSV files. Additionally, it includes examples of working with temporary files, counting vowels, and removing duplicates from text files.

Uploaded by

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

Programs

The document provides a series of Python code snippets demonstrating various file operations, including creating, reading, writing, and manipulating text and binary files. It covers tasks such as counting characters, words, and lines, searching for specific words, and handling CSV files. Additionally, it includes examples of working with temporary files, counting vowels, and removing duplicates from text files.

Uploaded by

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

Technicia Computer Education 1

1. File create karke data write karo

f = open("[Link]", "w")
[Link]("Hello Students")
[Link]()

2. File read karke print karo

f = open("[Link]", "r")
print([Link]())
[Link]()

3. Line by line read karo

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

4. File me append karo

f = open("[Link]", "a")
[Link]("\nNew Line Added")
[Link]()

5. Number of characters count karo

f = open("[Link]", "r")
data = [Link]()
print(len(data))
[Link]()

6. Number of words count karo

f = open("[Link]", "r")
data = [Link]()
words = [Link]()
print(len(words))
[Link]()

7. Number of lines count karo

f = open("[Link]", "r")
lines = [Link]()
print(len(lines))
[Link]()
Technicia Computer Education 2

8. File me specific word search karo

f = open("[Link]", "r")
data = [Link]()
if "Python" in data:
print("Found")
else:
print("Not Found")
[Link]()

9. Uppercase me convert karke write karo

f = open("[Link]", "r")
data = [Link]()
[Link]()

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

10. Copy content ek file se dusri file me

f1 = open("[Link]", "r")
data = [Link]()
[Link]()
f2 = open("[Link]", "w")
[Link](data)
[Link]()

11. File exist check karo

import os
if [Link]("[Link]"):
print("File exists")
else:
print("File not found")

12. Binary file me integer store karo

import struct
f = open("[Link]", "wb")
data = [Link]('i', 100)
[Link](data)
[Link]()

13. Binary file se integer read karo

import struct
Technicia Computer Education 3

f = open("[Link]", "rb")
data = [Link]()
num = [Link]('i', data)
print(num[0])
[Link]()

14. Temporary file create karo

import tempfile
f = [Link](mode='w+t')
[Link]("Temporary Data")
[Link](0)
print([Link]())
[Link]()

15. File me vowels count karo

f = open("[Link]", "r")
data = [Link]().lower()
count = 0
for ch in data:
if ch in "aeiou":
count += 1
print("Vowels:", count)
[Link]()

16. File me longest word find karo

f = open("[Link]", "r")
words = [Link]().split()
longest = max(words, key=len)
print("Longest word:", longest)
[Link]()

17. Har word ki frequency count karo

f = open("[Link]", "r")
words = [Link]().split()
d = {}
for w in words:
d[w] = [Link](w, 0) + 1
print(d)
[Link]()
Technicia Computer Education 4

[Link] me se digits alag file me store karo

f = open("[Link]", "r")
data = [Link]()
[Link]()
f2 = open("[Link]", "w")
for ch in data:
if [Link]():
[Link](ch)
[Link]()

18. File me lines reverse order me print karo

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

19. Har line ka first word print karo

f = open("[Link]", "r")
for line in f:
words = [Link]()
if words:
print(words[0])
[Link]()

20. File me se duplicate words remove karo

f = open("[Link]", "r")
words = [Link]().split()
[Link]()
unique = set(words)
f2 = open("[Link]", "w")
[Link](" ".join(unique))
[Link]()

21. File me lowercase ko uppercase me replace karo

f = open("[Link]", "r")
data = [Link]()
[Link]()
f2 = open("[Link]", "w")
[Link]([Link]())
[Link]()
Technicia Computer Education 5

22. Binary file me multiple integers store karo

import struct
f = open("[Link]", "wb")
for i in range(1, 6):
[Link]([Link]('i', i))
[Link]()

23. Binary file se sab integers read karo

import struct
f = open("[Link]", "rb")
while True:
data = [Link](4)
if not data:
break
num = [Link]('i', data)
print(num[0])
[Link]()

24. File me se special characters count karo

f = open("[Link]", "r")
data = [Link]()

count = 0
for ch in data:
if not [Link]() and not [Link]():
count += 1

print("Special chars:", count)


[Link]()

25. CSV file read karke specific column print karo

import csv

f = open("[Link]", "r")
reader = [Link](f)

for row in reader:


print(row[1]) # second column

[Link]()

26. Student names aur marks file me store karo


Technicia Computer Education 6

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

for i in range(3):
name = input("Name: ")
marks = input("Marks: ")
[Link](name + " " + marks + "\n")

[Link]()

27. File me se highest marks find karo

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

max_marks = 0
topper = ""

for line in f:
name, marks = [Link]()
if int(marks) > max_marks:
max_marks = int(marks)
topper = name

print("Topper:", topper)
[Link]()

28. Words starting with vowel count karo

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

count = 0
for w in words:
if w[0].lower() in "aeiou":
count += 1

print("Count:", count)
[Link]()

You might also like