-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile1.py
More file actions
63 lines (43 loc) · 1006 Bytes
/
Copy pathfile1.py
File metadata and controls
63 lines (43 loc) · 1006 Bytes
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
#create file
f1 = open("sample.txt", "x")
print("File created Successfully!")
f1.close()
#Writing data into file
f1 = open("sample.txt", "w")
f1.write("Hi File Handling in pyhton\n")
f1.write("File Handling is easy in Python\n")
f1.close()
#Reading data
f1 = open("sample.txt","r")
print("First Line:", f1.readline)
f1.close()
#Read all lines
f1 = open("sample.txt","r")
print("First Line:", f1.readlines)
f1.close()
#Append New Lines
f1 = open("sample.txt","a")
f1.write("This is appended line.\n")
f1.close()
#Binary
f1 = open("sample.txt","wb")
f1.write(b"This is binary data.\n")
f1.close()
#Read & Write
f1 = open("sample.txt", "r+")
print("Before:", f1.read())
f1.seek(0)
f1.write("Updated using 'r+' mode.\n")
f1.close()
#Write & Read
f1 = open("sample.txt", "w+")
f1.write("File updated using 'w+' mode.\n")
f1.seek(0)
print(f1.read())
f1.close()
#Append & Read
f1 = open("sample.txt", "a+")
f1.write("New line added using 'a+' mode.\n")
f1.seek(0)
print(f1.read())
f1.close()