|
49 | 49 | # - write to the stream: the portions of the data from the memory (ex: a variable) are transferred to the file |
50 | 50 |
|
51 | 51 | # three basic modes used to open the stream: |
52 | | -# - read mode: a stream opened in this mode allows read operations only; trying to write to the stream will cause an exception UnsupportedOperation |
53 | | -# - write mode: a stream opened in this mode allows write operations only; attempting to read the stream will cause UnsupportedOperation again |
54 | | -# - update mode: a stream opened in this mode allows both writes and reads |
| 52 | +# - READ mode: a stream opened in this mode allows read operations only; trying to write to the stream will cause an exception UnsupportedOperation |
| 53 | +# - WRITE mode: a stream opened in this mode allows write operations only; attempting to read the stream will cause UnsupportedOperation again |
| 54 | +# - UPDATE mode: a stream opened in this mode allows both writes and reads |
55 | 55 |
|
56 | 56 | # The stream behaves almost like a tape recorder |
57 | 57 | # ==> read something from a stream? a virtual head moves over the stream according to the number of bytes transferred from the stream |
|
71 | 71 |
|
72 | 72 |
|
73 | 73 |
|
74 | | -#### OPEN() |
| 74 | +#### OPEN(FILE, MODE, ENCODING) |
75 | 75 |
|
76 | 76 | ### to invoke the object (the file) |
77 | 77 |
|
|
80 | 80 |
|
81 | 81 | ### open() |
82 | 82 | # - very first operation performed on the stream |
83 | | -# - successful, the function returns a stream object |
84 | | -# - otherwise, an exception is raised, ex:FileNotFoundError |
| 83 | +# - successful ? ==> the function returns a stream object |
| 84 | +# - otherwise ==> an exception is raised, ex:FileNotFoundError |
85 | 85 |
|
86 | 86 |
|
87 | | -### file = specifies the name of the file to be associated with the stream |
| 87 | +### file |
| 88 | +# = specifies the name of the file to be associated with the stream |
88 | 89 |
|
89 | 90 |
|
90 | | -### mode = specifies the open mode used for the stream |
| 91 | +### mode |
| 92 | +# = specifies the open mode used for the stream |
91 | 93 | # - default opening mode is read in text mode |
92 | 94 | # - string filled with a sequence of characters, and each of them has its own special meaning: |
93 | 95 |
|
94 | | -## ==> r = open mode: read |
| 96 | +## ==> R = open mode: READ |
95 | 97 | # the stream will be opened in read mode |
96 | 98 | # the file associated with the stream must exist and has to be readable, otherwise exception raised |
97 | 99 |
|
98 | | -## ==> w = open mode: write |
| 100 | +## ==> W = open mode: WRITE |
99 | 101 | # the stream will be opened in write mode |
100 | 102 | # the file associated with the stream doesn't need to exist |
101 | 103 | # if it doesn't exist it will be created |
102 | 104 | # if it exists, it will be truncated to the length of zero (erased) |
103 | 105 | # if the creation isn't possible (ex: due to system permissions), exception |
104 | 106 |
|
105 | | -## ==>a = open mode: append |
106 | | -# the stream will be opened in append mode |
107 | | -# the file associated with the stream doesn't need to exist |
108 | | -# if it doesn't exist, it will be created |
109 | | -# if it exists the virtual recording head will be set at the end of the file, the previous content of the file remains UNTOUCHED ! |
110 | | - |
111 | | -## ==> r+ = open mode: read and update |
112 | | -# the stream will be opened in read and update mode |
113 | | -# the file associated with the stream MUST EXIST and has to be WRITEABLE, otherwise exception |
114 | | -# both read and write operations are allowed for the stream |
115 | | - |
116 | | -## ==> w+ open mode: write and update |
117 | | -# the stream will be opened in write and update mode |
118 | | -# the file associated with the stream doesn't need to exist |
119 | | -# if it doesn't exist, it will be created |
120 | | -# the previous content of the file remains UNTOUCHED |
121 | | -# both read and write operations are allowed for the stream. |
122 | | - |
123 | | -# If there is a letter b at the end of the mode string it means that the stream is to be opened in binary mode |
124 | | -# If the mode string ends with a letter t the stream is opened in text mode |
| 107 | +## ==> A = open mode: APPEND |
| 108 | +# the stream will be opened in append mode |
| 109 | +# the file associated with the stream doesn't need to exist |
| 110 | +# if it doesn't exist, it will be created |
| 111 | +# if it exists the virtual recording head will be set at the end of the file, the previous content of the file remains UNTOUCHED ! |
| 112 | + |
| 113 | +## ==> R+ = open mode: READ and UPDATE |
| 114 | +# the stream will be opened in read and update mode |
| 115 | +# the file associated with the stream MUST EXIST and has to be WRITEABLE, otherwise exception |
| 116 | +# both read and write operations are allowed for the stream |
| 117 | + |
| 118 | +## ==> W+ open mode: WRITE and UPDATE |
| 119 | +# the stream will be opened in write and update mode |
| 120 | +# the file associated with the stream doesn't need to exist |
| 121 | +# if it doesn't exist, it will be created |
| 122 | +# the previous content of the file remains UNTOUCHED |
| 123 | +# both read and write operations are allowed for the stream. |
| 124 | + |
| 125 | +# If there is a letter B at the end of the mode string it means that the stream is to be opened in BINARY mode |
| 126 | +# If the mode string ends with a letter T the stream is opened in TEXT mode |
125 | 127 | # TEXT MODE is the DEFAULT behaviour assumed when no binary/text mode specifier is used |
126 | 128 | # the successful opening of a file will set the current file position (the virtual reading/writing head) before |
127 | 129 | # the first byte of the file if the mode is not a and after the last byte of the file if the mode is set to a |
128 | 130 |
|
129 | 131 | ## encoding = specifies the encoding type, ex:UTF-8 when working with text files |
130 | 132 | # - default encoding depends on the platform used |
| 133 | + |
131 | 134 | # all the streams are divided into text and binary streams: |
132 | 135 | # - TEXT streams are structured in lines, they contain typographical characters (letters, digits, punctuation, etc), |
133 | 136 | # arranged in rows (lines), as seen with the naked eye when you look at the contents of the file in the editor |
134 | | -# - BINARY streams don't contain text but a sequence of bytes of any value, ex: an executable program, an image, |
135 | | -# an audio or a video clip, a database file, etc. |
| 137 | +# - BINARY streams don't contain text but a sequence of bytes of any value |
| 138 | +# ex: an executable program, an image, an audio or a video clip, a database file, etc. |
136 | 139 | # Because these files don't contain lines, the reads and writes relate to portions of data of any size. |
137 | 140 | # Hence the data is read/written byte by byte, or block by block, where the size of the block usually ranges from one to an arbitrarily chosen value |
138 | 141 |
|
139 | | - |
140 | 142 | # ! In Unix/Linux systems, the line ends are marked by a single character named LF (ASCII code 10) designated in Python programs as \n. |
141 | 143 | # Other operating systems, especially these derived from the prehistoric CP/M system (which applies to Windows family systems, too) |
142 | 144 | # use a different convention: the end of line is marked by a pair of characters, CR and LF (ASCII codes 13 and 10) which can be encoded as \r\n. |
|
162 | 164 | # the source code run in a Windows environment will work properly |
163 | 165 |
|
164 | 166 |
|
| 167 | + |
165 | 168 | ### 3 Exceptions where open() is not needed |
166 | | -import sys # 3 processes included int he sys module |
| 169 | +import sys # 3 processes included int the SYS MODULE |
167 | 170 |
|
168 | | -## ==> sys.stdin = standard input |
| 171 | +## ==> SYS.STDIN = standard input |
169 | 172 | # stdin stream is normally associated with the keyboard, pre-open for reading and regarded as the primary data source for the running programs |
170 | 173 | # the well-known input() function reads data from stdin by default |
171 | 174 |
|
172 | 175 |
|
173 | | -## ==> sys.stdout = standard output |
| 176 | +## ==> SYS.STDOUT = standard output |
174 | 177 | # stdout stream is normally associated with the screen, pre-open for writing, regarded as the primary target for outputting data by the running program |
175 | 178 | # the well-known print() function outputs the data to the stdout stream |
176 | 179 |
|
177 | 180 |
|
178 | | -# #==> sys.stderr = standard error output |
| 181 | +# #==> SYS.STDERR = standard error output |
179 | 182 | # stderr stream is normally associated with the screen, pre-open for writing, regarded as the primary place |
180 | 183 | # where the running program should send information on the errors encountered during its work |
181 | 184 |
|
|
199 | 202 | except Exception as exc: |
200 | 203 | print("Cannot open the file:", exc) |
201 | 204 | # --- |
202 | | - |
203 | 205 | ### 1. **Airbyte** |
204 | 206 | # - **Ressources en ligne** : |
205 | 207 | # - [Site officiel](https://airbyte.com/) — Documentation, guides d’utilisation. |
206 | 208 | # - [Tutoriel YouTube](https://www.youtube.com/watch?v=H6D3pWwX1JA) — Introduction à Airbyte. |
207 | 209 | # - [Blog Airbyte](https://airbyte.com/blog) — Cas d’usage, nouveautés. |
208 | 210 | # - **Ouvrages** : Aucun ouvrage dédié, mais la documentation est très complète. |
209 | 211 | # ... |
210 | | -#etc |
| 212 | +# etc |
211 | 213 | # ==> OK |
212 | 214 |
|
213 | 215 |
|
214 | 216 |
|
| 217 | + |
215 | 218 | #### CLOSE() |
216 | 219 | stream.close() |
217 | 220 | # to get rid of the object |
|
228 | 231 |
|
229 | 232 |
|
230 | 233 |
|
| 234 | + |
231 | 235 | #### DIAGNOSIS |
232 | 236 |
|
233 | 237 | # IOError object is equipped with a property named errno = error number |
|
286 | 290 |
|
287 | 291 |
|
288 | 292 |
|
| 293 | + |
289 | 294 | #### Gestion de l'erreur |
290 | 295 |
|
291 | | -### try/exept + errno |
| 296 | +### try/exept + ERRNO |
292 | 297 | # errno fournit des constantes pour représenter différents codes d'erreur |
293 | 298 | # Quand une erreur survient lors de l'ouverture d'un fichier, une exception IOError (ou OSError dans Python 3) est levée |
294 | 299 | # cette exception possède un attribut errno qui indique le type précis d'erreur |
|
323 | 328 | # The file doesn't exist. |
324 | 329 |
|
325 | 330 |
|
326 | | -### strerror() |
| 331 | +### STRERROR() |
327 | 332 | # comes from the os module |
328 | 333 | # 1 * argument = error number |
329 | 334 | # returns a string describing the meaning of the error |
|
0 commit comments