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...
# hgdiff.py
import subprocess
import sys
file = sys.argv[1]
subprocess.call(["gvimdiff", "<(hg cat %s)" % file, file])
When subprocess is called it merely passes <(hg cat file)
onto gvimdiff
as a filename.
So, is there any way to redirect a command as bash does? For simplicity's sake just cat a file and redirect it to diff:
diff <(cat file.txt) file.txt
Asked by: Dainton854 | Posted: 27-01-2022
Answer 1
It can be done. As of Python 2.5, however, this mechanism is Linux-specific and not portable:
import subprocess
import sys
file = sys.argv[1]
p1 = subprocess.Popen(['hg', 'cat', file], stdout=subprocess.PIPE)
p2 = subprocess.Popen([
'gvimdiff',
'/proc/self/fd/%s' % p1.stdout.fileno(),
file])
p2.wait()
That said, in the specific case of diff, you can simply take one of the files from stdin, and remove the need to use the bash-alike functionality in question:
file = sys.argv[1]
p1 = subprocess.Popen(['hg', 'cat', file], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['diff', '-', file], stdin=p1.stdout)
diff_text = p2.communicate()[0]
Answered by: Blake949 | Posted: 28-02-2022
Answer 2
There is also the commands module:
import commands
status, output = commands.getstatusoutput("gvimdiff <(hg cat file.txt) file.txt")
There is also the popen set of functions, if you want to actually grok the data from a command as it is running.
Answered by: Joyce878 | Posted: 28-02-2022Answer 3
This is actually an example in the docs:
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
which means for you:
import subprocess
import sys
file = sys.argv[1]
p1 = Popen(["hg", "cat", file], stdout=PIPE)
p2 = Popen(["gvimdiff", "file.txt"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
This removes the use of the linux-specific /proc/self/fd bits, making it probably work on other unices like Solaris and the BSDs (including MacOS) and maybe even work on Windows.
Answered by: Connie505 | Posted: 28-02-2022Answer 4
It just dawned on me that you are probably looking for one of the popen functions.
from: http://docs.python.org/lib/module-popen2.html
popen3(cmd[, bufsize[, mode]]) Executes cmd as a sub-process. Returns the file objects (child_stdout, child_stdin, child_stderr).
namaste, Mark
Answered by: Walter709 | Posted: 28-02-2022Similar questions
python - Redirect (>) command does not print some tail of output to file
I tried to print output result of my python program to log file using by this command:
./project.py > result.log
And when i opened result.log i found one problem. It missed some tail of output i think it's about 5 - 15 of the last lines
How to solve this problem?
Addtionnal information:
Language: Python
Termianl: iTerm2
OS: O...
Using Linux redirect to file command in Python
I want to write the output of the free command of Linux to a file using Python.
I have tried the following but it did not help:
from subprocess import call
call(["free",">","myfile"])
f = open('myfile','w')
f.write(subprocess.call(["free"]))
I am new to Python so can someone guide me here to write the free command output to a file using Python?
python - Run shell command pipe with output redirect lively ?
I am trying to run several lines of shell commands by Python, these shell commands are like:
sudo apt-get install xxx | sudo apt-get update
cd ~/somefolder
make && sudo make install
I want to run these lines of commands line by line, like:
for line in commands:
Run(line) # line could be any valid shell commands
inside function Run, I would lik...
python - Not able to redirect output of command to output file
I am writing a python script to take dump backup of Mongo. I want my output to be redirect to a text file that can be used for further reference.
I have tried sys.stdout but it is only printing the output of print command
sys.stdout
!/usr/bin/python3
import os
import time
import datetime
import sys
import subprocess
import glob
'''
mongo backup by python
'''
BKP_DIR = "/app...
Python Redirect command output to log file
This question already has answers here:
How redirect a shell command output to a Python script input ?
This is probably something really basic, but I can not find a good solution for it.
I need to write a python script that can accept input from a pipe like this:
$ some-linux-command | my_script.py
something like this:
cat email.txt | script.py
Or it will just be piped by my .forward file directly from sendmail. This means that the input file might be somet...
python - Redirect (>) command does not print some tail of output to file
I tried to print output result of my python program to log file using by this command:
./project.py > result.log
And when i opened result.log i found one problem. It missed some tail of output i think it's about 5 - 15 of the last lines
How to solve this problem?
Addtionnal information:
Language: Python
Termianl: iTerm2
OS: O...
Using Linux redirect to file command in Python
I want to write the output of the free command of Linux to a file using Python.
I have tried the following but it did not help:
from subprocess import call
call(["free",">","myfile"])
f = open('myfile','w')
f.write(subprocess.call(["free"]))
I am new to Python so can someone guide me here to write the free command output to a file using Python?
Redirect unix command output into a file in python
import sys,re,os
from subprocess import Popen, PIPE, call
newCmd = 'diff -qr -b -B '+sys.argv[1]+' '+sys.argv[2]+' --exclude-from='+sys.argv[3]+' | grep pattern1\|pattrern2 > outputFile'
ouT,erR = Popen(newCmd, shell=True).communicate()
print ouT,erR
ouT and erR are printing None, None and the outputFile is a blank file.
When i execute the same 'newCmd' in normal shell, ...
python - Redirect command line results to a texture (html) in real time
I would run a server and view real-time results in a textarea on a html page. Is it possible? For now I get to do it when the command is finished, but I would like to do while the server is running.
I tried:
r = subprocess.Popen(argServer, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
stdout, stderr = r.communicate()
print """<textarea>"""...
python - Run shell command pipe with output redirect lively ?
I am trying to run several lines of shell commands by Python, these shell commands are like:
sudo apt-get install xxx | sudo apt-get update
cd ~/somefolder
make && sudo make install
I want to run these lines of commands line by line, like:
for line in commands:
Run(line) # line could be any valid shell commands
inside function Run, I would lik...
python - Redirect command prompt to tkinter
I'm trying to redirect the powershell-output into a text box in Tkinter. I specifically want the print statements from the command prompt. does anyone know how I can go about this?
I followed this but it won't print to the text box.
my final function looks like this:
def start_script():
...
python - Redirect the output of custom Django command to the browser
I have created a custom command which takes a positional and a named optional argument. It does many checks, downloads a file, unzips it and populates the database with the unzipped data.
To make the things more convenient for the users I created a simple form and created a view:
from django.views.generic.edit import FormView
from django.core import management
from .forms import DownloadForm
class D...
python - Not able to redirect output of command to output file
I am writing a python script to take dump backup of Mongo. I want my output to be redirect to a text file that can be used for further reference.
I have tried sys.stdout but it is only printing the output of print command
sys.stdout
!/usr/bin/python3
import os
import time
import datetime
import sys
import subprocess
import glob
'''
mongo backup by python
'''
BKP_DIR = "/app...
cmd - Using '>' in the command prompt will redirect output for one python script, but not for another
I currently have a python script called tt.py added to the Windows PATH variable so that I can run it from any directory. tt.py consists of the following files and functions:
tt.py
Concatenate.py
cat
tac
CutPaste.py
cut
paste
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:
[1] Python instance method in C
[2] 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...
linux - 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 os.exec* functions?
This doesn't have to be portable, just work on Linux.
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?
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python