-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment14.py
More file actions
68 lines (52 loc) · 1.45 KB
/
Assignment14.py
File metadata and controls
68 lines (52 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#(Q.1)- Write a Python program to read last n lines of a file.
c = -1
f = open('test.txt','r')
content = f.readlines()
n = int(input("Enter the number of lines: "))
while c >=-n:
print(content[c],end="")
c = c - 1
f.close()
#(Q.2)- Write a Python program to count the frequency of words in a file.
with open('test.txt','r') as f:
count = f.read()
word = count.split()
s = set(word)
for n in s:
print(n,word.count(n))
#(Q.3)- Write a Python program to copy the contents of a file to another file.
with open('test.txt','r') as f1:
with open('test1.txt','w') as f2:
for line in f1:
f2.write(line)
#(Q.4)- Write a Python program to combine each line from first file with the corresponding line in second file.
with open('test.txt') as f3:
with open('test1.txt') as f4:
for line,line1 in zip(f3,f4):
print(line+line1)
#(Q.5- Write a Python program to write 10 random numbers into a file.
# Read the file and then sort the numbers and then store it to another file.
import random
def Rand(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
num = 10
start = 1
end = 10
res = Rand(start, end, num)
f = open('test3.txt', 'w')
for n in res:
f.write(str(n))
f.write("\n")
f.close()
f = open('test3.txt', 'r')
l = f.readlines()
f.close()
l.sort()
f = open('test4.txt', 'w')
for n in l:
f.write(n)
f.write("\n")
f.close()