多年前的这个问题符合我的要求:
但有没有办法使用子进程模块执行此操作?(据我所知是首选方式)
我查看了stackoverflow,python文档,以及许多谷歌搜索试图找到一种方法来使用stdin将所需的输入发送到p4进程,但我没有成功.我已经能够找到很多捕获子进程命令的输出,但是无法识别输入命令.
我对python一般都是新手,所以我很可能会遗漏一些明显的东西,但我不知道在这种情况下我不知道的是什么.
这是我到目前为止提出的代码:
descr = "this is a test description"
tempIn = tempfile.TemporaryFile()
tempOut = tempfile.TemporaryFile()
p = subprocess.Popen(["p4","change","-i"],stdout=tempOut, stdin=tempIn)
tempIn.write("change: New\n")
tempIn.write("description: " + descr)
tempIn.close()
(out, err) = p.communicate()
print out
Run Code Online (Sandbox Code Playgroud) 调用包含多个管道的命令Popen以便可以读取其输出的正确方法是什么?我试过了:
Popen(shlex.split("mycmd arg1 | mysecondcmd - | thirdcmd -", stdout=PIPE)")
但我不相信shlex.split就在这里.什么是正确的语法?
如何使用python在Ubuntu终端中保存来自"sudo dpkg -l"的数据,我试着这样做,但是它不起作用
import os
f = open('/tmp/dpgk.txt','w')
f.write(os.system('sudo dpkg -l'))
Run Code Online (Sandbox Code Playgroud) 在我的Python脚本中,这一行:
call("/Applications/BitRock\\ InstallBuilder\\ for\\ Qt\\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh")
Run Code Online (Sandbox Code Playgroud)
每次出错都会失败 OSError: [Errno 2] No such file or directory
但是,如果我写出该字符串的结果:
sys.stdout.write("/Applications/BitRock\\ InstallBuilder\\ for\\ Qt\\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh")
Run Code Online (Sandbox Code Playgroud)
我明白了:
/Applications/BitRock\ InstallBuilder\ for\ Qt\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh
Run Code Online (Sandbox Code Playgroud)
如果我把它直接放到终端,它就完美了.
我错过了什么?
我正在创建一个文件,然后在它上面做差异.
我想对上一步中创建的文件执行diff,但是我得到文件不存在的错误.
这是我的代码
os.popen("mysqldump --login-path=server1_mysql -e --opt --skip-lock-tables --skip-extended-insert -c %s > %s.sql" % (database, filename))
os.popen("diff %s %s > %s" % (weekly, filename, filename+".PATCH"))
Run Code Online (Sandbox Code Playgroud) 我试图在另一个python脚本中调用python脚本.目录是不同的.我试过了
import subprocess
subprocess.call("C:\temp\hello2.py", shell=True)
Run Code Online (Sandbox Code Playgroud)
但什么也没得到.这是行不通的.我回顾了很多论坛,但是当两个脚本都在同一个目录上时,它们都是关于调用它的.
我尝试在同一目录中同时使用这两个脚本.在这种情况下,我可以在Python.exe(通过cmd窗口)中运行模型,但不能在IDLE中运行.在IDLE中,我甚至没有收到错误消息.
我真的需要这样做,这样我就无法将其他脚本定义为不同的模块等.我需要在另一个脚本中调用脚本.
我的子流程代码有问题.该subprocess.Popen()工作正常,但是当我试图通过阅读它的输出stdout.read()是没有价值的阅读.
**import os
import signal
import subprocess
import threading
import sys
import commands
print commands.getoutput("hcitool dev")
print 'down'
commands.getoutput('hciconfig hci1 down')
print 'up'
commands.getoutput('hciconfig hci1 up')
commands.getoutput('killall hcitool')
stop = False
ping = subprocess.call('hcitool lescan', shell = False,
stdout=subprocess.PIPE,executable='/bin/bash')
for i in ping.stdout:
print i
def kill():
global stop
stop = True
os.kill(ping.pid, signal.SIGTERM)
threading.Timer(5, kill).start()
#while not stop:
# print 'now in while not loop'
# sys.stdout.write(ping.stdout.read(1))
print 'trying to print stdout'
out, err = …Run Code Online (Sandbox Code Playgroud) 使用命令行,我确认以下命令正确执行
echo '\c mydatabase;\i db-reset.sql' | psql -U postgres -h localhost
Run Code Online (Sandbox Code Playgroud)
但是,在Python中,我可以确认以下几行绝对没有,并返回状态代码0.
import subprocess
code = subprocess.call(r"echo '\c mydatabase;\i db-reset.sql' | psql -U postgres -h localhost", shell=True)
assert code == 0 # This comes to true
Run Code Online (Sandbox Code Playgroud)
从本质上讲,为什么使用子进程调用的命令实际上没有做任何事情?
我想删除所有文件和目录除了其中一些文件和目录使用
`subprocess.call(['rm','-r','!(new_models|creat_model.py|my_mos.tit)'])`
Run Code Online (Sandbox Code Playgroud)
但它会回馈信息
rm:无法删除`!(new_models | creat_model.py | my_mos.tit)':没有这样的文件或目录
我怎样才能解决这个问题?谢谢
为了输入minicom并保存它的日志,我使用"sudo minicom -C nameoffile",但是我想在循环中执行此操作,打开minicom可以通过使用子进程来完成但是我找不到任何东西来退出我的minicom循环并继续循环,因为你需要输入"ctrl-a,然后x"或"ctrl-a,然后q",之后必须按回车确认这一点.有人有任何想法或建议吗?