-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods-FILE.py
More file actions
191 lines (121 loc) · 2.42 KB
/
Methods-FILE.py
File metadata and controls
191 lines (121 loc) · 2.42 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
########## METHOD FILES ##########
### close()
# close the file
f = open('example.txt', 'w')
f.write('Hello')
f.close()
### detach()
# Split the stream from the buffer
import io
buffer = io.BytesIO(b"binary data")
raw_stream = buffer.detach()
### fileno()
# Gives the file number attributed by the OS
f = open('example.txt', 'r')
print(f.fileno())
# 3
f.close()
### flush()
# Checks that all is written in the file, forces the write
f = open('example.txt', 'w')
f.write('Text')
f.flush()
f.close()
### isatty()
# Check if the stream is interactive
import sys
print(sys.stdin.isatty())
# True
### read()
# Reads all the file
f = open('example.txt', 'r')
contenu = f.read()
print(contenu)
# Text
f.close()
### readable()
# Check if the file is readable
f = open('example.txt', 'r')
print(f.readable())
# True
f.close()
### readline()
# Reads one line at once
f = open('example.txt', 'r')
ligne = f.readline()
print(ligne)
# Ligne1
#
f.close()
### readlines()
# Reaeds all lines in a list
f = open('example.txt', 'r')
lignes = f.readlines()
print(lignes)
# ['Ligne1\n', 'Ligne2\n', 'Ligne3\n', 'Ligne4']
f.close()
### seek(INT)
# Change the read/writing position in the file
# example.txt BEFORE
'''Ligne1
Ligne2
Ligne3
Ligne4'''
f = open('exemple.txt', 'r+')
f.seek(0) # Return to the first character
f.write('Bonjour Madame') # replace existing text
f.close()
# example.txt AFTER
'''Bonjour Madame
Ligne3
Ligne4'''
### seekable()
# Checks if the file permits a position change
f = open('example.txt', 'r')
print(f.seekable())
# True
f.close()
### tell()
# Gives the current position in the file
f = open('example.txt', 'r')
print(f.tell())
# 0
f.read(5)
print(f.tell())
# 5 == > Position after reading
f.close()
### truncate()
# Resizes the file to the given size
# example.txt BEFORE
'''Ligne1
Ligne2
Ligne3
Ligne4'''
f = open('exemple.txt', 'w')
f.write('12345678') # replaces and writes '12345678'
# example.txt
'''12345678'''
f.truncate(5) # keeps only '12345'
f.close()
# example.txt AFTER
'''12345'''
### writable()
# Checks i the file is writable
f = open('example.txt', 'w')
print(f.writable())
# True
f.close()
### write()
# Writes in the file
f = open('example.txt', 'w')
f.write('Bonjour')
f.close()
### writelines()
# Writes lines in the file
f = open('example.txt', 'w')
f.writelines(['Line 1\n', 'Line 2\n', 'Line 3\n'])
f.close()
# example.txt AFTER
'''Line 1
Line 2
Line 3'''