如何使用Python Popen做多个参数? [英] How to do multiple arguments with Python Popen?
问题描述
我正在尝试制作一个带有按钮的PyGtk Gui.当用户按下此按钮时,gnome-terminal
提示用户输入密码.
I am trying to make a PyGtk Gui, that has a button. When the user presses this button, gnome-terminal
prompts the user to write their password.
然后它将克隆 Git存储库为gedit
JQuery代码片段.
Then it will clone this Git repository for gedit
JQuery snippets.
然后将js.xml
文件复制到/usr/share/gedit/plugins/snippets/js.xml
最后,它会强制删除Git存储库.
In the end, it forcefully removes the Git repository.
命令:
gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery
它在我的终端上正常工作.
It works fine in my terminal.
但是,通过GUI刚刚打开,我添加了密码,按Enter,然后再次关闭.
But, via the GUI it just opens, I add my password, press enter, and then it closes again.
我只想对第一个&&
这是我的Python函数(带有命令):
def on_install_jquery_code_snippet_for_gedit_activate(self, widget):
""" Install Jquery code snippet for Gedit. """
cmd="gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
close_fds=False)
self.status.set_text(p.stdout.read()) #show response in 'status
推荐答案
要直接回答您的问题,请阅读以下内容.但是您的程序存在很多问题,我在更好的实践"中介绍了其中的一些问题.
To directly answer your question, read below. But there's a lot of problems with your program, some of which I cover in "Better practice."
默认情况下,提供 subprocess.Popen
命令作为字符串列表.
By default, subprocess.Popen
commands are supplied as a list of strings.
但是,您也可以使用shell
参数执行命令其格式与在shell提示符下键入时的格式完全相同."
However, you can also you can use the shell
argument to execute a command "formatted exactly as it would be when typed at the shell prompt."
否:
>>> p = Popen("cat -n file1 file2")
是:
>>> p = Popen("cat -n file1 file2", shell=True)
>>> p = Popen(["cat", "-n", "file1", "file2"])
这两个选项之间存在许多差异,并且每个选项都有有效的用例.我不会尝试总结差异- Popen
文档在这方面做得很好.
There are a number of differences between these two options, and valid use cases for each. I won't attempt to summarize the differences- the Popen
docs already do an excellent job of that.
因此,对于您的命令,您将执行以下操作:
So, in the case of your commands, you'd do something like this:
cmd = "gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
close_fds=False)
更好的练习
但是,使用Python作为许多系统命令的包装并不是一个好主意.至少,您应该将命令分解为单独的Popens,以便可以适当地处理非零退出.实际上,此脚本似乎更适合作为Shell脚本.但是,如果您坚持使用Python,则有更好的做法.
Better practice
However, using Python as a wrapper for many system commands is not really a good idea. At the very least, you should be breaking up your commands into separate Popens, so that non-zero exits can be handled adequately. In reality, this script seems like it'd be much better suited as a shell script. But if you insist on Python, there are better practices.
os
模块应该代替对rm
的调用,并且cp
.而且,尽管我没有经验,但您可能希望查看 GitPython 之类的工具来与Git存储库进行交互
The os
module should take the place of calls to rm
and cp
. And while I have no experience with it, you might want to look at tools like GitPython to interact with Git repositories.
最后,您应谨慎呼叫gnome-terminal
和sudo
.并非所有的GNU/Linux用户都运行Ubuntu,也不是每个人都安装了sudo
或GNOME终端模拟器.在当前形式下,如果出现以下情况,您的脚本将崩溃,但无济于事:
Lastly, you should be careful about making calls to gnome-terminal
and sudo
. Not all GNU/Linux users run Ubuntu, and not everyone has sudo
, or the GNOME terminal emulator installed. In its current form, your script will crash, rather unhelpfully, if:
- 未安装
sudo
命令 - 用户不在
sudoers
组 - 用户未使用GNOME或其默认终端仿真器
- 未安装Git
- The
sudo
command is not installed - The user is not in the
sudoers
group - The user doesn't use GNOME, or its default terminal emulator
- Git is not installed
如果您愿意假设您的用户正在运行Ubuntu,则比直接调用gnome-terminal
更好的选择是调用x-terminal-emulator
,因为它将调用他们已安装的任何终端模拟器(例如,用户使用xfce4-terminal
).
If you're willing to assume your users are running Ubuntu, calling x-terminal-emulator
is a much better option than calling gnome-terminal
directly, as it will call whatever terminal emulator they've installed (e.g. xfce4-terminal
for users of Xubuntu).
这篇关于如何使用Python Popen做多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!