Create text viewer for text in filename. Return error message if file cannot be read. Otherwise calls view_text with contents of the file.
(parent, title, filename, encoding, modal=True, wrap='word',
_utest=False)
| 162 | |
| 163 | |
| 164 | def view_file(parent, title, filename, encoding, modal=True, wrap='word', |
| 165 | _utest=False): |
| 166 | """Create text viewer for text in filename. |
| 167 | |
| 168 | Return error message if file cannot be read. Otherwise calls view_text |
| 169 | with contents of the file. |
| 170 | """ |
| 171 | try: |
| 172 | with open(filename, encoding=encoding) as file: |
| 173 | contents = file.read() |
| 174 | except OSError: |
| 175 | showerror(title='File Load Error', |
| 176 | message=f'Unable to load file {filename!r} .', |
| 177 | parent=parent) |
| 178 | except UnicodeDecodeError as err: |
| 179 | showerror(title='Unicode Decode Error', |
| 180 | message=str(err), |
| 181 | parent=parent) |
| 182 | else: |
| 183 | return view_text(parent, title, contents, modal, wrap=wrap, |
| 184 | _utest=_utest) |
| 185 | return None |
| 186 | |
| 187 | |
| 188 | if __name__ == '__main__': |