MCPcopy Index your code
hub / github.com/ipython/ipython / read_py_file

Function read_py_file

IPython/utils/openpy.py:62–82  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

60 yield from it
61
62def 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
84def 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.

Callers 1

pfileMethod · 0.90

Calls 2

strip_encoding_cookieFunction · 0.85
readMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…