popen

When should I use subprocess.Popen instead of os.popen?

人走茶凉 提交于 2020-01-15 06:18:11
问题 Seems both executes a subprocess and create a pipe to do in/out, just that the subprocess is newer. My question is, is there any function that subprocess.Popen can do while os.popen cannot, so that we need the new module subprocess ? Why Python language didn't choose to enhance os.popen but created a new module? 回答1: Short answer: Never use os.popen , always use subprocess! As you can see from the Python 2.7 os.popen docs: Deprecated since version 2.6: This function is obsolete. Use the

When should I use subprocess.Popen instead of os.popen?

戏子无情 提交于 2020-01-15 06:16:29
问题 Seems both executes a subprocess and create a pipe to do in/out, just that the subprocess is newer. My question is, is there any function that subprocess.Popen can do while os.popen cannot, so that we need the new module subprocess ? Why Python language didn't choose to enhance os.popen but created a new module? 回答1: Short answer: Never use os.popen , always use subprocess! As you can see from the Python 2.7 os.popen docs: Deprecated since version 2.6: This function is obsolete. Use the

Using popen() to invoke a shell command?

邮差的信 提交于 2020-01-14 14:01:48
问题 When running the following code through xcode I get inconsistent behavior. Sometimes it prints the git version correctly, other times it doesn't print anything. The return code from the shell command is always 0 though. Any ideas on why this might be? What am I doing wrong? #define BUFFER_SIZE 256 int main (int argc, const char * argv[]) { FILE *fpipe; char *command="/opt/local/bin/git --version"; char line[BUFFER_SIZE]; if ( !(fpipe = (FILE*)popen(command, "r")) ) { // If fpipe is NULL

popen to pass binary data between processes

走远了吗. 提交于 2020-01-14 05:49:06
问题 I am facing issue in passing binary data between processes. My program opens a pipe to ffmpeg using popen() and tries to capture the output and then stream it as HTTP server. I am doing something like this ffmpeg -i "input_video.avi" -ab 56 -ar 44100 -b 1500000 -r 25 -s 800x600 -f flv - (Output filename "-" diverts the output to stdout) After opening I am using fread() to read the pipe. I can read it and my program streams content, when I downloaded the file on my browser, it completed, but

Popen and python

混江龙づ霸主 提交于 2020-01-13 09:21:13
问题 Working on some code and I'm given the error when running it from the command prompt... NameError: name 'Popen' is not defined but I've imported both import os and import sys . Here's part of the code exepath = os.path.join(EXE File location is here) exepath = '"' + os.path.normpath(exepath) + '"' cmd = [exepath, '-el', str(el), '-n', str(z)] print 'The python program is running this command:' print cmd process = Popen(cmd, stderr=STDOUT, stdout=PIPE) outputstring = process.communicate()[0]

subprocess模块

我的梦境 提交于 2020-01-12 18:25:19
用法 import os import subprocess r = subprocess.Popen('ls',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) # subprocess.Popen(cmd,shell=True,subprocess.stdout,subprocess.stderr) # cmd : 代表系统命令 # shell = True 代表这条命令是 系统命令,告诉操作系统,将cmd当成系统命令去执行 # stdout 是执行完系统命令之后,用于保存结果的一个管道 # stderr 是执行完系统命令之后,用于保存错误结果的一个管道 stdout = r.stdout.read().decode('gbk') stderr = r.stderr.read().decode('gbk') print('正确的返回结果:',stdout) print('错误的返回结果:',stderr) print('错误的返回结果:',stderr) 以前我一直用 os.system() 处理一些系统管理任务,因为我认为那是运行linux命令最简单的方式. 我们能从Python官方文档里读到应该用 subprocess 模块来运行系统命令. subprocess 模块允许我们创建子进程,连接他们的输入/输出

Python实现cmd命令连续执行

女生的网名这么多〃 提交于 2020-01-12 02:07:36
之前是想写一个微信控制程序,通过登录网页微信,可以直接执行命令行代码。也不用ssh登录了,想法很方便。 但是现实很残酷,微信登录这块基本没有问题,已经有大佬写好了,但是命令行执行遇到问题了。 运行cmd 开始时,使用os.popen()执行命令,但是该命令需要手动修改运行目录。此方案被我直接丢弃了。 单开进程 那么自然想到通过启动进程的方式来实现,Python有对进程的封装 subprocess ,可以通过创建Popen对象来实现。我只要单开一个bash,与它进行交互就好啦。 简单实现如下: ​ p = subprocess.Popen('/bin/bash', shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) ​ while True: c = input() c += os.linesep p.stdin.write(c.encode('utf8')) print(out_s.decode('utf8'), end='') 然后,马上就有遇到问题了,输出流一直拿不到内容,被阻塞了。 刷新缓冲区 被阻塞有两种情况,一输入流阻塞,所以没有输出,二输出流阻塞。看到网上有的将输入流关闭就可以了: p.stdin.close() 但是关闭后就不能再次运行命令了

How to open a file on mac OSX 10.8.2 in python

心已入冬 提交于 2020-01-11 14:42:38
问题 I am writing a python code on eclipse and want to open a file that is present in Downloads folder. I am using MAC OSX 10.8.2. I tried with f=os.path.expanduser("~/Downloads/DeletingDocs.txt") and also with ss=subprocess.Popen("~/Downloads/DeletingDocs.txt",shell=True) ss.communicate() I basically want to open a file in subprocess, to listen to the changes in the opened file.But, the file is not opening in either case. 回答1: from os.path import baspath, expanduser filepath = abspath(expanduser(

Get a signal once a subprocess ends

狂风中的少年 提交于 2020-01-11 13:08:10
问题 I am using the subprocess.Popen function to run a command line. Without having to use Popen.wait(), I want to check the subprocess after it has finished using Popen.poll(). Any suggestions on how to do this? import subprocess job = subprocess.Popen('command line', shell = True) print(job.poll()) As it is, I get job.poll() printed before the subprocess starts. I want it to wait until it ends. I don't want to use wait because the rest of the user interface becomes unusable until the process

Get a signal once a subprocess ends

99封情书 提交于 2020-01-11 13:03:54
问题 I am using the subprocess.Popen function to run a command line. Without having to use Popen.wait(), I want to check the subprocess after it has finished using Popen.poll(). Any suggestions on how to do this? import subprocess job = subprocess.Popen('command line', shell = True) print(job.poll()) As it is, I get job.poll() printed before the subprocess starts. I want it to wait until it ends. I don't want to use wait because the rest of the user interface becomes unusable until the process