Next time, you'll be happier if instead of using
printstatements at all you use theloggingmodule from the start. It provides the control you want and you can have it write to stdout while that's still where you want it.Many people here have suggested redirecting stdout. This is an ugly solution. It mutates a global and—what's worse—it mutates it for this one module's use. I would sooner make a regex that changes all
print footoprint >>my_file, fooand setmy_fileto either stdout or an actual file of my choosing.- If you have any other parts of the application that actually depend on writing to stdout (or ever will in the future but you don't know it yet), this breaks them. Even if you don't, it makes reading your module look like it does one thing when it actually does another if you missed one little line up top.
- Chevron print is pretty ugly, but not nearly as ugly as temporarily changing
sys.stdoutfor the process. - Very technically speaking, a regex replacement isn't capable of doing this right (for example, it could make false positives if you are inside of a multiline string literal). However, it's apt to work, just keep an eye on it.
os.systemis virtually always inferior to using thesubprocessmodule. The latter needn't invoke the shell, doesn't pass signals in a way that usually is unwanted, and can be used in a non-blocking manner.
Next time, you'll be happier if instead of using
printstatements at all you use theloggingmodule from the start. It provides the control you want and you can have it write to stdout while that's still where you want it.os.systemis virtually always inferior to using thesubprocessmodule. The latter needn't invoke the shell, doesn't pass signals in a way that usually is unwanted, and can be used in a non-blocking manner.
Next time, you'll be happier if instead of using
printstatements at all you use theloggingmodule from the start. It provides the control you want and you can have it write to stdout while that's still where you want it.Many people here have suggested redirecting stdout. This is an ugly solution. It mutates a global and—what's worse—it mutates it for this one module's use. I would sooner make a regex that changes all
print footoprint >>my_file, fooand setmy_fileto either stdout or an actual file of my choosing.- If you have any other parts of the application that actually depend on writing to stdout (or ever will in the future but you don't know it yet), this breaks them. Even if you don't, it makes reading your module look like it does one thing when it actually does another if you missed one little line up top.
- Chevron print is pretty ugly, but not nearly as ugly as temporarily changing
sys.stdoutfor the process. - Very technically speaking, a regex replacement isn't capable of doing this right (for example, it could make false positives if you are inside of a multiline string literal). However, it's apt to work, just keep an eye on it.
os.systemis virtually always inferior to using thesubprocessmodule. The latter needn't invoke the shell, doesn't pass signals in a way that usually is unwanted, and can be used in a non-blocking manner.
Next time, you'll be happier if instead of using
printstatements at all you use theloggingmodule from the start. It provides the control you want and you can have it write to stdout while that's still where you want it.os.systemis virtually always inferior to using thesubprocessmodule. The latter needn't invoke the shell, doesn't pass signals in a way that usually is unwanted, and can be used in a non-blocking manner.