![Python find word in file and print line](https://knopkazmeya.com/12.png)
![python find word in file and print line python find word in file and print line](https://www.codegrepper.com/codeimages/python-get-text-file-line-count.png)
Sys.stdout = f # Change the standard output to the file we created. Original_stdout = sys.stdout # Save a reference to the original standard output with open( 'filename.txt', 'w') as f: Print( 'This message will be displayed on the screen.') Let's see an example where we change stdout to be a file:
![python find word in file and print line python find word in file and print line](https://i.stack.imgur.com/AAxsC.png)
It is a file-like object, meaning it has methods that allow Python to read and write from it like an actual file. In Python, a reference to the standard output can be obtained using the stdout object of the sys module. If the standard output is redirected to a specific file, the text specified in the print() function will be written to that file instead of being displayed on the screen.
![python find word in file and print line python find word in file and print line](https://miro.medium.com/max/1200/1*nPNMuSAxnXqRe6cT8_kEbg.png)
However, the standard output can be redirected to other locations, such as files, for convenience. Instead, it sends text to a location called the standard output stream, also known as stdout.Īll UNIX systems have three main pipes - standard input pipe ( stdin), standard output pipe ( stdout) and standard error pipe ( stderr).īy default, the standard output pipe points to the interactive window used to execute the program, so we normally see text printed out on the screen. It was not hard-coded in such a way that specified text can only be written to the display. In Python, the print() function is more flexible than you might think. It is often more flexible to perform this redirection from within the Python script itself. However, with this method, all output of the script is written to a file. If we open our newly created output.txt, we'll see the following contents: Hallo
![Python find word in file and print line](https://knopkazmeya.com/12.png)