Make a temporary python file, return filename and filehandle. Parameters ---------- src : string or list of strings (no need for ending newlines if list) Source code to be written to the file. ext : optional, string Extension for the generated file. Returns
(src: str, ext: str='.py')
| 116 | |
| 117 | |
| 118 | def temp_pyfile(src: str, ext: str='.py') -> str: |
| 119 | """Make a temporary python file, return filename and filehandle. |
| 120 | |
| 121 | Parameters |
| 122 | ---------- |
| 123 | src : string or list of strings (no need for ending newlines if list) |
| 124 | Source code to be written to the file. |
| 125 | ext : optional, string |
| 126 | Extension for the generated file. |
| 127 | |
| 128 | Returns |
| 129 | ------- |
| 130 | (filename, open filehandle) |
| 131 | It is the caller's responsibility to close the open file and unlink it. |
| 132 | """ |
| 133 | fname = tempfile.mkstemp(ext)[1] |
| 134 | with open(Path(fname), "w", encoding="utf-8") as f: |
| 135 | f.write(src) |
| 136 | f.flush() |
| 137 | return fname |