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

9 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import typer 1abcdefgh

2from typing_extensions import Annotated 1abcdefgh

3 

4 

5def main(file: Annotated[typer.FileBinaryWrite, typer.Option()]): 1abcdefgh

6 first_line_str = "some settings\n" 1abcdefgh

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

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

9 # Then you can write the bytes 

10 file.write(first_line_bytes) 1abcdefgh

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

12 second_line = b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o" 1abcdefgh

13 file.write(second_line) 1abcdefgh

14 print("Binary file written") 1abcdefgh

15 

16 

17if __name__ == "__main__": 1abcdefgh

18 typer.run(main) 1abcdefgh