Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Standard streams
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===2000 - : Python (2 or 3) === The following example, written in [[Python (programming language)|Python]], shows how to redirect the standard input both to the standard output and to a text file. <syntaxhighlight lang="python" line="1"> #!/usr/bin/env python import sys # Save the current stdout so that we can revert sys.stdout # after we complete our redirection stdin_fileno = sys.stdin stdout_fileno = sys.stdout # Redirect sys.stdout to the file sys.stdout = open("myfile.txt", "w") ctr = 0 for inps in stdin_fileno: ctrs = str(ctr) # Prints to the redirected stdout () sys.stdout.write(ctrs + ") this is to the redirected --->" + inps + "\n") # Prints to the actual saved stdout handler stdout_fileno.write(ctrs + ") this is to the actual --->" + inps + "\n") ctr = ctr + 1 # Close the file sys.stdout.close() # Restore sys.stdout to our old saved file handler sys.stdout = stdout_fileno </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)