Coverage for docs_src / parameter_types / file / tutorial004_py310.py: 100%
10 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
1import typer 1abcdefg
3app = typer.Typer() 1abcdefg
6@app.command() 1abcdefg
7def main(file: typer.FileBinaryWrite = typer.Option(...)): 1abcdefg
8 first_line_str = "some settings\n" 1abcdefg
9 # You cannot write str directly to a binary file, you have to encode it to get bytes
10 first_line_bytes = first_line_str.encode("utf-8") 1abcdefg
11 # Then you can write the bytes
12 file.write(first_line_bytes) 1abcdefg
13 # This is already bytes, it starts with b"
14 second_line = b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o" 1abcdefg
15 file.write(second_line) 1abcdefg
16 print("Binary file written") 1abcdefg
19if __name__ == "__main__": 1abcdefg
20 app() 1abcdefg