-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnine.py
More file actions
81 lines (63 loc) · 2.08 KB
/
nine.py
File metadata and controls
81 lines (63 loc) · 2.08 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
69
70
71
72
73
74
75
76
77
78
79
80
81
# -----------------------------------------------FILE I/O-------------------------------------------------------
'''
TYPE OF FILES.
There are 2 types of files:
1. Text files (.txt, .c, etc)
2. Binary files (.jpg, .data, etc)
Python has a lot of functions for reading, updating, and deleting files.
'''
# -----------------------------------------------OPENING AND READING A FILE IN PYTHON-------------------------------------------------------
'''
a = "a very long string with emails"
emails = []
3 seconds
'''
f = open("read.txt", "r")
data = f.read()
print(data)
print("using readline")
print(f.readline()) # We can also use f.readline() function to read one full line at a time. Read one line from the file.
f.close()
# -----------------------------------------------MODES OF OPENING A FILE------------------------------------------
'''
r-open for reading
w-open for writing
a- open for appending
+ - open for updating.
'rb' will open for read in binary mode.
'rt' will open for read in text mode.
'''
# -----------------------------------------------WRITE FILES IN PYTHON-------------------------------------------------------
st = "Hey John you are amazing"
# f = open("write.txt", "w")
# f.write(st)
# f.close()
# -----------------------------------------------MORE FILE FUNCTIONS-------------------------------------------------------
# append
f = open("write.txt", "a")
f.write(st)
f.close()
print('------more funs----------')
f = open("read.txt")
# lines = f.readlines()
# print(lines, type(lines))
line1 = f.readline()
print(line1, type(line1))
line2 = f.readline()
print(line2, type(line2))
line3 = f.readline()
print(line3, type(line3))
line4 = f.readline()
print(line4, type(line4))
line5 = f.readline()
print(line5 =="")
line = f.readline()
while(line != ""):
print(line)
line = f.readline()
f.close()
# -----------------------------------------------WITH STATEMENT-------------------------------------------------------
# The best way to open and close the file automatically is the with statement.
print('---------with statement------------')
with open("read.txt") as f:
print(f.read())