Open a text file with proper handling. Args: filename: Path to file. mode: Open mode. encoding: Encoding to use. errors: Error handling. Returns: file: Opened file object.
(filename, mode, encoding=None, errors=None)
| 1190 | |
| 1191 | |
| 1192 | def openTextFile(filename, mode, encoding=None, errors=None): |
| 1193 | """Open a text file with proper handling. |
| 1194 | |
| 1195 | Args: |
| 1196 | filename: Path to file. |
| 1197 | mode: Open mode. |
| 1198 | encoding: Encoding to use. |
| 1199 | errors: Error handling. |
| 1200 | |
| 1201 | Returns: |
| 1202 | file: Opened file object. |
| 1203 | """ |
| 1204 | # Do not attempt to create files with what we consider |
| 1205 | # illegal filenames. |
| 1206 | if "w" in mode: |
| 1207 | is_legal, illegal_reason = isLegalPath(filename) |
| 1208 | if not is_legal: |
| 1209 | raise NuitkaFilenameError(illegal_reason) |
| 1210 | |
| 1211 | # Doesn't exist anymore for Python3.7 or later. |
| 1212 | if python_version >= 0x370: |
| 1213 | mode = mode.replace("U", "") |
| 1214 | |
| 1215 | if str is bytes: |
| 1216 | return codecs.open(filename, mode, encoding=encoding, errors=errors) |
| 1217 | else: |
| 1218 | return io.open(filename, mode, encoding=encoding, errors=errors) |
| 1219 | |
| 1220 | |
| 1221 | def putTextFileContents(filename, contents, encoding=None): |
no test coverage detected
searching dependent graphs…