Redirect stderr to stdout on exec-ed process from python?
In a bash script, I can write:
exec 2>&1 exec someprog
And the stderr output of
someprog would be redirected to stdout.
Is there any way to do a similar thing using python's
This doesn't have to be portable, just work on Linux.
Asked by: Walter642 | Posted: 30-11-2021
/bin/ls with a bogus argument so that it complains to stderr.
$ python -c "import os; os.execl('/bin/ls', '', 'ffweew')" 1>/dev/null : ffweew: No such file or directory $ python -c "import os; os.execl('/bin/ls', '', 'ffweew')" 2>/dev/null $ python -c "import os; os.dup2(1, 2); os.execl('/bin/ls', '', 'ffweew')" 1>/dev/null $ python -c "import os; os.dup2(1, 2); os.execl('/bin/ls', '', 'ffweew')" 2>/dev/null : ffweew: No such file or directory $
First two invocations prove that
ls does not write to stdout, and writes the error message to stderr.
In the 3rd and the 4th invocation, the Python program duplicates file descriptor 1 as file descriptor 2, achieving the desired effect.Answered by: Ada883 | Posted: 01-01-2022
bash - Redirect command to input of another in Python
I would like to replicate this in python: gvimdiff <(hg cat file.txt) file.txt (hg cat file.txt outputs the most recently committed version of file.txt) I know how to pipe the file to gvimdiff, but it won't accept another file: $ hg cat file.txt | gvimdiff file.txt - Too many edit arguments: "-" Getting to the python part... # hg...
Python + Django page redirect
How do I accomplish a simple redirect (e.g. cflocation in ColdFusion, or header(location:http://) for PHP) in Django?
python - How to pass information using an HTTP redirect (in Django)
I have a view that accepts a form submission and updates a model. After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page. How can I "pass" this message to the other page? HttpResponseRedirect only accepts a URL. I've seen this done bef...
python - Django: Redirect to previous page after login
I'm trying to build a simple website with login functionality very similar to the one here on SO. The user should be able to browse the site as an anonymous user and there will be a login link on every page. When clicking on the login link the user will be taken to the login form. After a successful login the user should be taken back to the page from where he clicked the login link in the first place. I'm guessing that I ...
How to redirect the output of .exe to a file in python?
In a script , I want to run a .exe with some command line parameters as "-a",and then redirect the standard output of the program to a file? How can I implement that?
python - How to Redirect To Same Page on Failed Login
The Django framework easily handles redirecting when a user fails to log in properly. However, this redirection goes to a separate login page. I can set the template to be the same as the page I logged in on, but none of my other objects exist in the new page. For example, I have a front page that shows a bunch of news articles. On the sidebar is a login form. When the user logs in, but fails to authenticate, I wou...
cgi - How to redirect to another page in Python in CGIAr
If I want to redirect the user from a cgi script to a HTML page using core libraries from the following: import cgi Please could someone point me in the right direction. Nothing more. Simply redirect from cgi script to html page. How ever you would do this Python. If you have to physically write out the HTTP Response Headers to achieve this, then I would appreciate any i...
How to redirect stderr in Python? Via Python C API?
This is a combination of my two recent questions:  Python instance method in C  How to redirect stderr in Python? I would like to log the output of both stdout and stderr from a python script. The thing I want to ask is...
How to redirect stderr in Python?
I would like to log all the output of a Python script. I tried: import sys log =  class writer(object): def write(self, data): log.append(data) sys.stdout = writer() sys.stderr = writer() Now, if I "print 'something' " it gets logged. But if I make for instance some syntax error, say "print 'something# ", it wont get logged - it will go into the console instead. Ho...
python - How to set cookies with redirect in Pylons
In light of the cookie-handling bugs affecting Safari and Chrome (see this thread), and Pylons implementation of redirect_to as an exception, is it possible to reliably set a tracking cookie and redirect at the same time? Is the META refresh method looked down upon?