在python中复制修改的du命令 [英] Replicate a modified du command in python
本文介绍了在python中复制修改的du命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在shell中执行的命令。我想在Python中获得相同的结果。我可以使用 os
模块来做到这一点吗?我在这里使用 grep -v
,因为一些文件名也有这种模式。请注意,我不想从shell调用它。
Here is the command I execute in shell. I want to get the same results in Python. Can I do this using the os
module and how? I'm using grep -v
here as some file names also have that pattern. Please note that i dont want to invoke this from shell.
du -ah 2> >(grep -v "permission denied") |grep [1-9][0-9]G | grep -v [0-9][0-9]K|grep -v [0-9][0-9]M|sort -nr -k 1| head -50
推荐答案
你可以使用这个python程序。它不会在shell中产生任何子进程。
You can use this python program. It does not spawn any child processes in the shell.
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
import subprocess
import os
import argparse
def files_larger_than_no_child_process(min_bytes, count):
"""Return the top count files that are larger than the given min_bytes"""
# A list that will have (size, name) tuples.
file_info = []
for root, dirs, files in os.walk("."):
for f in files:
file_path = os.path.abspath(os.path.realpath(os.path.join(root, f)))
try:
size = os.path.getsize(file_path)
# Discard all smaller files than the given threshold
if size > min_bytes:
file_info.append((size,file_path))
except OSError as e:
pass
# Sort the files with respect to their sizes
file_info = sorted(file_info, key=lambda x: x[0], reverse=True)
# Print the top count entries
for l in file_info[:count]:
print(l[0], " ", l[1])
def main():
parser = argparse.ArgumentParser("""Prints the top files that are larger than the
given bytes in the current directory recusrively.""")
parser.add_argument("min_bytes",help="Print files larger than this value",
type=int)
parser.add_argument("count",help="Print at most the given number of files",
type=int)
args = parser.parse_args()
files_larger_than_no_child_process(args.min_bytes, args.count)
if __name__ == "__main__":
main()
这篇关于在python中复制修改的du命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文