fast →💬 Create a quick text configecho "some settings" > config.txt 💬 Add another line to the config to test itecho "some more settings" >> config.txt 💬 Now run your programpython main.py --config config.txt Config line: some settings
For writing text, you can use typer.FileTextWrite:
importtyperfromtyping_extensionsimportAnnotateddefmain(config:Annotated[typer.FileTextWrite,typer.Option()]):config.write("Some config written by the app")print("Config written")if__name__=="__main__":typer.run(main)
Tip
Prefer to use the Annotated version if possible.
importtyperdefmain(config:typer.FileTextWrite=typer.Option(...)):config.write("Some config written by the app")print("Config written")if__name__=="__main__":typer.run(main)
This would be for writing human text, like:
some settings
la cigüeña trae al niño
...not to write binary bytes.
Check it:
fast →python main.py --config text.txt Config written
💬 Check the contents of the filecat text.txt Some config written by the app
To read binary data you can use typer.FileBinaryRead.
You will receive bytes from it.
It's useful for reading binary files like images:
importtyperfromtyping_extensionsimportAnnotateddefmain(file:Annotated[typer.FileBinaryRead,typer.Option()]):processed_total=0forbytes_chunkinfile:# Process the bytes in bytes_chunkprocessed_total+=len(bytes_chunk)print(f"Processed bytes total: {processed_total}")if__name__=="__main__":typer.run(main)
Tip
Prefer to use the Annotated version if possible.
importtyperdefmain(file:typer.FileBinaryRead=typer.Option(...)):processed_total=0forbytes_chunkinfile:# Process the bytes in bytes_chunkprocessed_total+=len(bytes_chunk)print(f"Processed bytes total: {processed_total}")if__name__=="__main__":typer.run(main)
To write binary data you can use typer.FileBinaryWrite.
You would write bytes to it.
It's useful for writing binary files like images.
Have in mind that you have to pass bytes to its .write() method, not str.
If you have a str, you have to encode it first to get bytes.
importtyperfromtyping_extensionsimportAnnotateddefmain(file:Annotated[typer.FileBinaryWrite,typer.Option()]):first_line_str="some settings\n"# You cannot write str directly to a binary file, you have to encode it to get bytesfirst_line_bytes=first_line_str.encode("utf-8")# Then you can write the bytesfile.write(first_line_bytes)# This is already bytes, it starts with b"second_line=b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o"file.write(second_line)print("Binary file written")if__name__=="__main__":typer.run(main)
Tip
Prefer to use the Annotated version if possible.
importtyperdefmain(file:typer.FileBinaryWrite=typer.Option(...)):first_line_str="some settings\n"# You cannot write str directly to a binary file, you have to encode it to get bytesfirst_line_bytes=first_line_str.encode("utf-8")# Then you can write the bytesfile.write(first_line_bytes)# This is already bytes, it starts with b"second_line=b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o"file.write(second_line)print("Binary file written")if__name__=="__main__":typer.run(main)
fast →python main.py --file binary.dat Binary file written
💬 Check the binary file was createdls ./binary.dat ./binary.dat
It's automatically set for you by using the classes above.
Read more about it below.
encoding: to force a specific encoding, e.g. "utf-8".
lazy: delay I/O operations. Automatic by default.
By default, when writing files, Click will generate a file-like object that is not yet the actual file. Once you start writing, it will go, open the file and start writing to it, but not before. This is mainly useful to avoid creating the file until you start writing to it. It's normally safe to leave this automatic. But you can overwrite it setting lazy=False. By default, it's lazy=True for writing and lazy=False for reading.
atomic: if true, all writes will actually go to a temporal file and then moved to the final destination after completing. This is useful with files modified frequently by several users/programs.
typer.FileTextWrite is actually just a convenience class. It's the same as using typer.FileText with mode="w".
But it's probably shorter and more intuitive as you can get it with autocompletion in your editor by just starting to type typer.File... just like the other classes.
You can override the mode from the defaults above.
For example, you could use mode="a" to write "appending" to the same file:
importtyperfromtyping_extensionsimportAnnotateddefmain(config:Annotated[typer.FileText,typer.Option(mode="a")]):config.write("This is a single line\n")print("Config line written")if__name__=="__main__":typer.run(main)
Tip
Prefer to use the Annotated version if possible.
importtyperdefmain(config:typer.FileText=typer.Option(...,mode="a")):config.write("This is a single line\n")print("Config line written")if__name__=="__main__":typer.run(main)
Tip
As you are manually setting mode="a", you can use typer.FileText or typer.FileTextWrite, both will work.
Check it:
fast →python main.py --config config.txt Config line written
💬 Run your program a couple more times to see how it appends instead of overwritingpython main.py --config config.txt Config line written
python main.py --config config.txt Config line written
💬 Check the contents of the file, it should have each of the 3 lines appendedcat config.txt This is a single line This is a single line This is a single line
These are technical details about why the different types/classes provided by Typer.
But you don't need this information to be able to use them. You can skip it.
Typer provides you these different types (classes) because they inherit directly from the actual Python implementation that will be provided underneath for each case.
This way your editor will give you the right type checks and completion for each type.
Even if you use lazy. When you use lazy Click creates a especial object to delay writes, and serves as a "proxy" to the actual file that will be written. But this especial proxy object doesn't expose the attributes and methods needed for type checks and completion in the editor. If you access those attributes or call the methods, the "proxy" lazy object will call them in the final object and it will all work. But you wouldn't get autocompletion for them.
But because these Typer classes inherit from the actual implementation that will be provided underneath (not the lazy object), you will get all the autocompletion and type checks in the editor.