-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText File Handling.py
151 lines (97 loc) · 3.49 KB
/
Text File Handling.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
print("This program performs operations on a text file called 'my_file.txt'")
#Creating text file
def Create_file():
f = open("my_file.txt",'w')
print("\nThe text file 'my_file.txt' has been successfully created!")
string = input("Enter a para: ")
f.write(string)
print("Content has been successfully added to the file")
f.close()
#Reading the text file
def Read_file():
f = open("my_file.txt",'r')
print("\nThe content of 'my_file.txt' is:")
content = f.read()
print(content)
f.close()
#Capitalize the first letter of every word and print the word
def Caps_file():
f = open("my_file.txt",'r')
para = f.read()
list1 = para.split()
new_string = ""
for i in list1:
new_string = new_string + (i.capitalize()) + " "
print(new_string)
f.close()
#Print the content of the file in reverse order
def Reverse_file():
f = open("my_file.txt",'r')
para = f.read()
new_string = ""
for i in range(-1,-(len(para))-1,-1):
new_string += para[i]
print(new_string)
f.close()
while True:
print("\nA: Create a text file to old the content")
print("B: Read the text file created and print its content")
print("C: Capitalize the first letter of every word of the content")
print("D: Reverse the entire string")
print("E: Exit the program")
choice = input("\nEnter your choice(A/B/C/D/E): ")
if choice.upper() == "A":
Create_file()
elif choice.upper() == "B":
Read_file()
elif choice.upper() == "C":
Caps_file()
elif choice.upper() == "D":
Reverse_file()
elif choice.upper() == "E":
print("The program will end now! Thank you!")
break
else:
print("\nEnter a correct choice!")
continue
"""OUTPUT:
This program performs operations on a text file called 'my_file.txt'
A: Create a text file to old the content
B: Read the text file created and print its content
C: Capitalize the first letter of every word of the content
D: Reverse the entire string
E: Exit the program
Enter your choice(A/B/C/D/E): a
The text file 'my_file.txt' has been successfully created!
Enter a para: I love pig latin
Content has been successfully added to the file
A: Create a text file to old the content
B: Read the text file created and print its content
C: Capitalize the first letter of every word of the content
D: Reverse the entire string
E: Exit the program
Enter your choice(A/B/C/D/E): b
The content of 'my_file.txt' is:
I love pig latin
A: Create a text file to old the content
B: Read the text file created and print its content
C: Capitalize the first letter of every word of the content
D: Reverse the entire string
E: Exit the program
Enter your choice(A/B/C/D/E): c
I Love Pig Latin
A: Create a text file to old the content
B: Read the text file created and print its content
C: Capitalize the first letter of every word of the content
D: Reverse the entire string
E: Exit the program
Enter your choice(A/B/C/D/E): d
nital gip evol I
A: Create a text file to old the content
B: Read the text file created and print its content
C: Capitalize the first letter of every word of the content
D: Reverse the entire string
E: Exit the program
Enter your choice(A/B/C/D/E): e
The program will end now! Thank you!
"""