Direct Standard Error
Contents |
a stderr redirect stderr to a stdout redirect stderr and stdout to a file redirect stderr and stdout to stdout redirect stderr and stdout to stderr 1 'represents' stdout and 2 stderr. redirect standard error and output to different files A little note for seeing this things: with the less command you can view redirect standard error and output to /dev/null both stdout (which will remain on the buffer) and the stderr that will be printed on the screen, but erased as redirect standard error and standard out to file you try to 'browse' the buffer. 3.2 Sample: stdout 2 file This will cause the ouput of a program to be written to a file.
ls -l > ls-l.txtHere, a file called
Redirect Standard Error And Output To File Windows
'ls-l.txt' will be created and it will contain what you would see on the screen if you type the command 'ls -l' and execute it. 3.3 Sample: stderr 2 file This will cause the stderr ouput of a program to be written to a file.
grep da * 2> grep-errors.txtHere, a file called 'grep-errors.txt' will be created and it will contain what you would see the redirect standard error unix stderr portion of the output of the 'grep da *' command. 3.4 Sample: stdout 2 stderr This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.
grep da * 1>&2Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways. 3.5 Sample: stderr 2 stdout This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.
grep * 2>&1Here, the stderr portion of the command is sent to stdout, if you pipe to less, you'll see that lines that normally 'dissapear' (as they are written to stderr) are being kept now (because they're on stdout). 3.6 Sample: stderr and stdout 2 file This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.
rm -f $(find / -name core) &> /dev/nullThis (thinking on the cron entry) will delete every file called 'core' in any directory. Notice that you should be pretty sure of what a command is doing if you are going to wipe it's output. Next Previous Contents
>20.3. Applications
Redirect Standard Error Dos
within a script (see Example 3-1 and Example 3-2) and sending it as input to another file, command, program, or script.
Linux Pipe Standard Error
>Each open file gets assigned a file descriptor. [2] The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. For opening additional files, there remain descriptors 3 http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html to 9. It is sometimes useful to assign one of these additional file descriptors to stdin, stdout, or stderr as a temporary duplicate link. [3] This simplifies restoration to normal after complex redirection and reshuffling (see Example 20-1).
COMMAND_OUTPUT > # Redirect stdout to a file. # Creates the file if not present, otherwise overwrites it. ls -lR > dir-tree.list # Creates a file containing a http://www.tldp.org/LDP/abs/html/io-redirection.html listing of the directory tree. : > filename # The > truncates file "filename" to zero length. # If file not present, creates zero-length file (same effect as 'touch'). # The : serves as a dummy placeholder, producing no output. > filename # The > truncates file "filename" to zero length. # If file not present, creates zero-length file (same effect as 'touch'). # (Same result as ": >", above, but this does not work with some shells.) COMMAND_OUTPUT >> # Redirect stdout to a file. # Creates the file if not present, otherwise appends to it. # Single-line redirection commands (affect only the line they are on): # -------------------------------------------------------------------- 1>filename # Redirect stdout to file "filename." 1>>filename # Redirect and append stdout to file "filename." 2>filename # Redirect stderr to file "filename." 2>>filename # Redirect and append stderr to file "filename." &>filename # Redirect both stdout and stderr to file "filename." # This operator is now functional, as of Bash 4, final release. M>N # "M" is a file descriptor, which defaults to 1, if not explicitly set. # "N" is a filename. # File descriptor "M" is redirect to file "N." M>&N # "M" is a file descriptor, which defaults to 1, if not set. # "N" is another file descriptor. #============================================================================== # Redirecting stdout, one line at a time. LOGFILE=script.log ehere for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company http://stackoverflow.com/questions/4877796/direct-standard-error-to-same-file-and-standard-output Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million http://www.diveintopython.net/scripts_and_streams/stdin_stdout_stderr.html programmers, just like you, helping each other. Join them; it only takes a minute: Sign up direct standard error to same file and standard output? up vote 1 down vote favorite 1 I have something like f=open('out.txt','w') standard error print >>f, 'action=%r'%action in one of my Python programs. Is it possible to direct standard error to the same file as standard output? Thanks! python file file-io share|improve this question asked Feb 2 '11 at 17:38 reckoner 9832831 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted You could do this... import sys f = open("error.log", "w") sys.stderr = f print "raising exception" raise Exception, "find this in direct standard error error.log" Or to answer your question more directly, import sys f = open("logall.txt", "w") sys.stderr = f sys.stdout = f print "find this in logall.txt" raise Exception, "find this in logall.txt" Although I don't necessarily recommend the latter. share|improve this answer answered Feb 2 '11 at 17:48 senderle 61.2k11115152 why don't you recommend it? –reckoner Feb 2 '11 at 17:53 If you want to write to a file, I think it's better to just write to that file directly with f.write(), rather than making a global change like sys.stdout = f, which affects all print statements. Probably just my opinion though. –senderle Oct 1 '12 at 1:31 add a comment| up vote 1 down vote You might just say this: sys.stderr = sys.stdout But I'd wager if your script will ever be maintained by someone else, you might cause some confusion. Rather than doing this inside the script, would it be an acceptable solution to do it from the calling shell? myscript 2>&1 share|improve this answer answered Feb 2 '11 at 18:08 philo 9241922 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Em
concept of standard input, standard output, and standard error. This section is for the rest of you. Standard output and standard error (commonly abbreviated stdout and stderr) are pipes that are built into every UNIX system. When you print something, it goes to the stdout pipe; when your program crashes and prints out debugging information (like a traceback in Python), it goes to the stderr pipe. Both of these pipes are ordinarily just connected to the terminal window where you are working, so when a program prints, you see the output, and when a program crashes, you see the debugging information. (If you're working on a system with a window-based Python IDE, stdout and stderr default to your "Interactive Window".) Example10.8.Introducing stdout and stderr >>> for i in range(3): ... print 'Dive in' Dive in Dive in Dive in >>> import sys >>> for i in range(3): ... sys.stdout.write('Dive in') Dive inDive inDive in >>> for i in range(3): ... sys.stderr.write('Dive in') Dive inDive inDive in As you saw in Example6.9, "Simple Counters", you can use Python's built-in range function to build simple counter loops that repeat something a set number of times. stdout is a file-like object; calling its write function will print out whatever string you give it. In fact, this is what the print function really does; it adds a carriage return to the end of the string you're printing, and calls sys.stdout.write. In the simplest case, stdout and stderr send their output to the same place: the Python IDE (if you're in one), or the terminal (if you're running Python from the command line). Like stdout, stderr does not add carriage returns for you; if you want them, add them yourself. stdout and stderr are both file-like objects, like the ones you discussed in Section10.1, "Abstracting input sources", but they are both write-only. They have no read method, only write. Still, they are file-like objects, and you can assign any other file- or file-like object to them to redirect their output. Example10.9.Redirecting output [you@localhost kgp]$ python stdout.py Dive in [you@localhost kgp]$ cat out.log This message will be logged instead of displayed(On Windows, you can use type instead of cat to