Read a Python file, using the encoding declared inside the file. Parameters ---------- filename : str The path to the file to read. skip_encoding_cookie : bool If True (the default), and the encoding declaration is found in the first two lines, that line will
(filename: str | Path, skip_encoding_cookie: bool = True)
| 60 | yield from it |
| 61 | |
| 62 | def read_py_file(filename: str | Path, skip_encoding_cookie: bool = True) -> str: |
| 63 | """Read a Python file, using the encoding declared inside the file. |
| 64 | |
| 65 | Parameters |
| 66 | ---------- |
| 67 | filename : str |
| 68 | The path to the file to read. |
| 69 | skip_encoding_cookie : bool |
| 70 | If True (the default), and the encoding declaration is found in the first |
| 71 | two lines, that line will be excluded from the output. |
| 72 | |
| 73 | Returns |
| 74 | ------- |
| 75 | A unicode string containing the contents of the file. |
| 76 | """ |
| 77 | filepath = Path(filename) |
| 78 | with open(filepath) as f: # the open function defined in this module. |
| 79 | if skip_encoding_cookie: |
| 80 | return "".join(strip_encoding_cookie(f)) |
| 81 | else: |
| 82 | return f.read() |
| 83 | |
| 84 | def read_py_url(url: str, errors: str = 'replace', skip_encoding_cookie: bool = True) -> str: |
| 85 | """Read a Python file from a URL, using the encoding declared inside the file. |
no test coverage detected
searching dependent graphs…