查找带有子进程的命令无法正常运行Shell = True [英] find command with subprocess not working with out Shell=True

查看:106
本文介绍了查找带有子进程的命令无法正常运行Shell = True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中包含以下几行.我嵌入了一段简短的一行,该行会提取早于10分钟的文件列表.我的子流程一直失败,几乎没有错误.当我给Shell = True时,它似乎可以工作,但是我读到使用该选项是非常冒险的,而且我对Python还是很陌生,不想弄乱我不了解的东西.我曾尝试使用该find语句及其周围的内容更改引号,但它无济于事.您能建议我如何使用find命令获取文件列表吗? 我使用这些查找和子流程组合研究了其他问题,但未找到任何通配符目录匹配项.我找不到解决方案.

I have below lines in my code. I have embedded a short a line which fetches the list of files that are older than 10 mins. My sub process have been failing with few errors. It seems to work when I give Shell=True, but I read that it is a very risky to use that option and I am very new to the Python, Don't want to mess up with something I am not understanding. I have tried changing quotes with in and around that find statement, but its not helping me. can you please suggest how could I get the list of files using find command. I have looked into other questions with these find and subprocess combinations, I did not find any wildcard directory matches. I could not find solution for this.

cmd = 'find /myapp/uat/aws/6.3/domains/*/appnodes/*/*/log/bwappnode.log -type f -mmin +10'

 apps_in_proc = subprocess.Popen(cmd,stdout=subprocess.PIPE, universal_newlines=True)

推荐答案

(如果它与shell=True一起使用,而不是没有),则意味着该模式由shell=True展开.

if it works with shell=True, and not without, that means that the pattern is expanded with shell=True.

要模拟此行为,只需使用glob.glob并编写如下的命令参数列表:

To emulate this behaviour just use glob.glob and compose your command argument list like this:

cmd = ['find'] + glob.glob('/myapp/uat/aws/6.3/domains/*/appnodes/*/*/log/bwappnode.log') + ['-type','f','-mmin','+10']

使用纯python可以很容易地编写出以下内容:

Which could be written very easily in pure python:

import glob,os,time
current = time.time()
old_files = [x for x in glob.glob("/myapp/uat/aws/6.3/domains/*/appnodes/*/*/log/bwappnode.log") if current - os.path.getmtime(x) > 600]

这篇关于查找带有子进程的命令无法正常运行Shell = True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆