I have a Python file like this:
#!/usr/local/bin/python3.9
print("Writing to file...")
print({"test": 1})
I'm running it from the shell like this:
./script.py > output.json
Which writes the following to output.json
:
Writing to file...
{'test': 1}
The behaviour I want to achieve is for the first print statement to be printed to the console only, and for the second print statement to be written to the file. i.e. I want some things to be printed normally, and some things to be written to a file. Like this:
#!/usr/local/bin/python3.9
print("Writing to file...") # only prints to console
print({"test": 1}) # only writes to output.json
How can I modify my code to achieve this? It's important that the shell command is unchanged.