简体   繁体   English

Python subprocess.Popen 使用 unix 'find' 命令给出错误

[英]Python subprocess.Popen giving error with unix 'find' command

I am trying to get the largest 20 files in a directory that are older than 60 days by passing the unix 'find' command to Python's subprocess.Popen.我试图通过将 unix 'find' 命令传递给 Python 的 subprocess.Popen 来获取超过 60 天的目录中最大的 20 个文件。 This is what I have tried:这是我尝试过的:

# Get largest files older than 60 days
cmd_string = 'find {0} -type f -mtime +60 -size +300M -exec du -sh {{}} \; | sort -rh | head -n20'.format(rmnt)
print(cmd_string)
cmd_args = shlex.split(cmd_string, posix=False)
print(cmd_args[12])
find_out = subprocess.Popen(cmd_args, stdout=subprocess.PIPE).stdout

where rmnt is a directory name (eg. '/mnt/active').其中 rmnt 是目录名称(例如,'/mnt/active')。 The print statements return the command correctly:打印语句正确返回命令:

find /mnt/active -type f -mtime +60 -size +300M -exec du -sh {} \; | sort -rh | head -n20
\;

But I am getting this error:但我收到此错误:

find: missing argument to `-exec'

I thought that the problem was due to the special character "\" but it is being printed as expected.我认为问题出在特殊字符“\”上,但它正在按预期打印。

Update 1.更新 1。

I found this similar question: find: missing argument to `-exec' when using subprocess我发现了这个类似的问题: find: missing argument to `-exec' when using subprocess

It deals with subprocess.call rather than subprocess.Popen but the answer that "there is no shell to interpret and remove that backslash" seems to apply here also.它处理 subprocess.call 而不是 subprocess.Popen 但“没有 shell 可以解释和删除该反斜杠”的答案似乎也适用于此。 However, with the backslash removed I still get an error:但是,删除反斜杠后,我仍然收到错误消息:

find: paths must precede expression: |

This error suggests that the backslash is not being passed to the find command此错误表明反斜杠未传递给 find 命令

For anyone that has a similar issue, there are 2 problems in my code.对于任何有类似问题的人,我的代码中有两个问题。

1. 1.

By default, subprocess.Popen uses shell=False and therefore there is no shell to interpret and remove the backslash as explained here: find: missing argument to `-exec' when using subprocess默认情况下,subprocess.Popen 使用shell=False ,因此没有 shell 来解释和删除反斜杠,如下所述: find: missing argument to `-exec' when using subprocess

2. 2.

To use a pipe with the subprocess module, you have to pass shell=True which is a security hazard.要将 pipe 与 subprocess 模块一起使用,您必须传递shell=True ,这是一个安全隐患。 Therefore, the commands should be separated into 2 or more commands as explained here: How to use `subprocess` command with pipes因此,应将命令分成 2 个或更多命令,如下所述: 如何使用带有管道的 `subprocess` 命令

The code that worked for me is:对我有用的代码是:

# Get largest files older than 60 days
find_cmd = 'find {0} -type f -mtime +60 -size +300M -exec du -sh {{}} ;'.format(rmnt)
find_args = shlex.split(find_cmd)
sort_cmd = 'sort -rh'
sort_args = shlex.split(sort_cmd)

find_process = subprocess.Popen(find_args, stdout=subprocess.PIPE)
sort_process = subprocess.Popen(sort_args, stdin=find_process.stdout,stdout=subprocess.PIPE)
find_process.stdout.close()
sort_out = sort_process.stdout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM