-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathChapter_10th.py
110 lines (80 loc) · 2.15 KB
/
Chapter_10th.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
# Example 1
# make sure you have demofile.txt file in your working directory
f = open("demofile.txt", "r")
print(f.read())
# Output
# C:\Users\My Name>python demo_file_open.py
# Hello! Welcome to demofile.txt
# This file is for testing purposes.
# Good Luck!
# Example 2
f = open("demofile.txt", "r")
print(f.read(5))
# Output
# C:\Users\My Name>python demo_file_open2.py
# Hello
# Example 3
f = open("demofile.txt", "r")
print(f.readline())
# Output
# C:\Users\My Name>python demo_file_readline.py
# Hello! Welcome to demofile.txt
# Example 4
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
# Output
# C:\Users\My Name>python demo_file_readline2.py
# Hello! Welcome to demofile.txt
# This file is for testing purposes.
# Example 5
f = open("demofile.txt", "r")
for x in f:
print(x)
# Output
# C:\Users\My Name>python demo_file_readline3.py
# Hello! Welcome to demofile.txt
# This file is for testing purposes.
# Good Luck!
# Example 6
f = open("demofile.txt", "r")
print(f.readline())
f.close()
# Output
# C:\Users\My Name>python demo_file_close.py
# Hello! Welcome to demofile.txt
# Example 7
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
# open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
# Output
# C:\Users\My Name>python demo_file_append.py
# Hello! Welcome to demofile2.txt
# This file is for testing purposes.
# Good Luck!Now the file has more content!
# Example 8
f = open("demofile3.txt", "w")
f.write(" I have deleted the content!")
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
# Output
# C:\Users\My Name>python demo_file_write.py
# I have deleted the content!
# Example 9
import os
os.remove("demofile.txt")
# Example 10
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
# Example 11
# Create myfolder in working directory before executing code below.
import os
os.rmdir("myfolder")