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

11 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1import typer 1abcdefghi

2from typing_extensions import Annotated 1abcdefghi

3 

4app = typer.Typer() 1abcdefghi

5 

6 

7@app.command() 1abcdefghi

8def main(file: Annotated[typer.FileBinaryWrite, typer.Option()]): 1abcdefghi

9 first_line_str = "some settings\n" 1abcdefghi

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

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

12 # Then you can write the bytes 

13 file.write(first_line_bytes) 1abcdefghi

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

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

16 file.write(second_line) 1abcdefghi

17 print("Binary file written") 1abcdefghi

18 

19 

20if __name__ == "__main__": 1abcdefghi

21 app() 1abcdefghi