Coverage for docs_src/parameter_types/file/tutorial004.py: 100%

8 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import typer 1abcdefghi

2 

3 

4def main(file: typer.FileBinaryWrite = typer.Option(...)): 1abcdefghi

5 first_line_str = "some settings\n" 1abcdefghi

6 # You cannot write str directly to a binary file, you have to encode it to get bytes 

7 first_line_bytes = first_line_str.encode("utf-8") 1abcdefghi

8 # Then you can write the bytes 

9 file.write(first_line_bytes) 1abcdefghi

10 # This is already bytes, it starts with b" 

11 second_line = b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o" 1abcdefghi

12 file.write(second_line) 1abcdefghi

13 print("Binary file written") 1abcdefghi

14 

15 

16if __name__ == "__main__": 1abcdefghi

17 typer.run(main) 1abcdefghi