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

11 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1from typing import Annotated 1abcdefghij

2 

3import typer 1abcdefghij

4 

5app = typer.Typer() 1abcdefghij

6 

7 

8@app.command() 1abcdefghij

9def main(file: Annotated[typer.FileBinaryWrite, typer.Option()]): 1abcdefghij

10 first_line_str = "some settings\n" 1abcdefghij

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

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

13 # Then you can write the bytes 

14 file.write(first_line_bytes) 1abcdefghij

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

16 second_line = b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o" 1abcdefghij

17 file.write(second_line) 1abcdefghij

18 print("Binary file written") 1abcdefghij

19 

20 

21if __name__ == "__main__": 1abcdefghij

22 app() 1abcdefghij