-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_writefile.py
58 lines (51 loc) · 1.33 KB
/
read_writefile.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
# Reading and writing to files using python
import sys
import time
# Welcoming user
print("")
print("Welcome To Python File Reader and Writer.")
print("")
print("1. Read from File")
print("")
print("2. Add Data to File")
print("")
print("3. Write to New File")
print("")
option = input(str("Please Enter an Option :"))
print("")
option = int(option)
if option == 1:
filename = input(str("Please Enter Filename :"))
file = open(filename, "r")
file = file.read()
print("File Starts")
print("")
print(file)
print("")
print("File End.")
elif option == 2:
print("")
filename = input(str("Please Enter Filename : "))
print("")
filecontent = input(str("Please Enter Content for the File : "))
print("")
file = open(filename, "a") # use 'a+' for append and write to file
file.write("\n")
file.write(filecontent)
file.close()
print("")
print("File has been Saved.")
elif option == 3:
print("")
filename = input(str("Please Enter Filename : "))
print("")
filecontent = input(str("Please Enter Content for the File : "))
print("")
file = open(filename, "w") # use 'w' for only writing to new file
file.write("\n")
file.write(filecontent)
file.close()
print("")
print("File has been Saved.")
else:
print("Please Enter a Valid Option..")