简体   繁体   中英

Python executable to Linux list files with sizes

I need my test.py to display the following in LINUX shell - list all files in directory - descending order of file size in bytes (must display bytes) - show total files and total size at end (X files X total size) - does not include sub directories or files in other subdirectories

here is my python executable

    #!/usr/bin/env python
    import subprocess
    subprocess.call(["ls", "-l", "-S", "-s"])

this shows me the files and their size in descending order but it includes folders/subdirectories which I do not want

additionally replacing the subprocess.call with subprocess.call(["find", "-type","f"]) shows me only the files without the unneeded dates and times but I don't know how to get the info I want.

My python code:

#!/usr/bin/env python 
import subprocess, os, operator 
directory='e:\\Programs/Cyg/home/Dylan/test'       
list=os.listdir(directory) 
pairs=[] 
for file in list:
    if os.path.isfile: 
        location=os.path.join(directory, file)   
        size=os.path.getsize(location) pairs.append((file,size))  
pairs.sort(key=operator.itemgetter(0)) 
for pair in pairs:
   print (pair)

Current directory

from os import listdir
from os.path import isfile, getsize
from operator import itemgetter

files = [(f, getsize(f)) for f in listdir('.') if isfile(f)]
files.sort(key=itemgetter(1), reverse=True)

for f, size in files:
    print '%d %s' % (size, f)
print '(%d files %d total size)' % (len(files), sum(f[1] for f in files))

Other directories

from os import listdir
from operator import itemgetter
from os.path import isfile, getsize, join, basename

def listfiles(dir):
    paths = (join(dir, f) for f in listdir(dir))
    files = [(basename(p), getsize(p)) for p in paths if isfile(p)] 
    files.sort(key=itemgetter(1), reverse=True)

    for f, size in files:
        print '%d %s' % (size, f)
    print '(%d files %d total size)' % (len(files), sum(f[1] for f in files))

You can pipe the output to grep to ignore directories.

from subprocess import Popen,PIPE

directory = 'path' 

p1 = Popen(["ls",directory, "-Ssp"], stdout=PIPE)

p2 = Popen(["grep", "-v", '/$'], stdin=p1.stdout, stdout=PIPE)  

p1.stdout.close()
out, _err = p2.communicate()

files = out.splitlines()
total = files.pop(0)
print(total)
print(len(files))

In your own code you were sorting by the wrong key, you need to put the file size first in the tuple making sure to actually call isfile .

import  operator
from os import path, listdir

 directory = 'path' 

lst = listdir(directory)
pairs = []
for f in lst:
    if os.path.isfile(path.join(directory, f)):
        location = path.join(directory, f)
        size = os.path.getsize(location)
        pairs.append((size, f))

pairs.sort(key=operator.itemgetter(0),reverse=True)

print(len(pairs))
print(sum(s for s,_ in pairs))

for size, f in pairs:
    print(size, f)

Also to sort from highest to lowest you need to use reverse=True . If you want to ignore copies you can use if not f.endswith("~") or use the -B flag with ls to ignore backups. To change the output size for files using ls you would need to use something like --block-size 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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