2

I have gotten stuck. I need to write code using Python to find a file by its size and add its name and size to a list. I have a program which searches a directory for a file by name. I need to make another flag with get opts to do a search by size.

import getopt
import sys
import os
from os import listdir, walk
from os.path import isfile, join

def find_by_name(name, path, result): #Define a function to search the file by it's name
    result = []
    for root, dirs, files in os.walk(path):
        if name in files:
            result.append(os.path.join(name)) #Join the file to the list called result
        else:
            print ("Nothing was found by %s" % name)
        return result
def main():
    path_dir = raw_input("Select the directory you want to search: ")
    results = []
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'n:y:d:')
    except getopt.GetoptError as err:
        print (err)
        sys.exit

    for o, a in opts:
        if o in ("-n", "--name"):
           pro = find_by_name(a, path_dir, results)
if __name__ == "__main__":
    main()
aschultz
  • 1,658
  • 3
  • 20
  • 30
Jade
  • 283
  • 3
  • 9
  • 18
  • 1
    I feel like this is a good on-topic question but it's difficult to parse because of the unfamiliarity of the asker with English. Can someone translate? – Adam Smith May 15 '14 at 22:03
  • He wants the size of the file – deweyredman May 15 '14 at 22:04
  • You can find filesize by using os.stat function as described [here][1] [1]: http://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python – deweyredman May 15 '14 at 22:06
  • @AdamSmith i just wanted to parse a file by its size like a do with function find_by_name. Sorry for my bad english :) – Jade May 15 '14 at 22:08
  • 2
    @user3642703 No need to apologize for your bad English -- I can all but guarantee that you speak my language better than I speak yours! – Adam Smith May 15 '14 at 22:09
  • Sorry about that. For some reason it submitted my answer as a comment... – deweyredman May 15 '14 at 22:10

6 Answers6

3

os.walk gives you the path and filename. you can then use

stats = os.stat(path+name)
stats.st_size

to get the file size in bytes. so you could just change up your current function to something like:

def find_by_size(size, path):
    result = []
    for root, dirs, files in os.walk(path):
        if os.stat(path+name).st_size == size:
            result.append((os.path.join(name), stats.st_size))
        else:
            print ("Nothing of size %d was found" % size)
        return result

also you don't need to pass result in, since you're just replacing it with an empty list. Python can return lists from a function.

3
def matched_files(base_directory):
    for root, dirs, files in os.walk(path):
        if name in files:
           yield os.path.join(root,name) #Join the file to the list called result

print sorted(matched_files("/some/path"),key=os.path.getsize) #sort files matching name by size

I think will work ... plus it simplifies your matching program alot ... by turning it into a generator

if you are trying to match all files that are a given size regardless of name ... this might not be the best solution ... but you could probably make it work easy enough

really if you want to find all files of a certain size ... just plain old bash/sed/awk would probably work best

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

You can get the file size using the following snippet.

import os
os.path.getsize('./path/to/file')

So you could get the size of every file and then sort the files according to size.

soarjay
  • 641
  • 4
  • 17
1

Use os.stat to find the filesize.

filestats = os.stat(filename)
filesize = filestats.st_size
deweyredman
  • 1,440
  • 1
  • 9
  • 12
1

To get the size of the file, use:

os.path.getsize(path)

returns the value in bytes

So:

def get_files_by_size(path, size):
""" Returns a list of files that are the size provided """
    result = []

    for root, dirs, files in os.walk(path):
        for file in files:
            path = os.path.join(root, file)

            if os.path.getsize(path) == size:
                result.append(path)
    return result
andrewgrz
  • 435
  • 3
  • 6
0

I think you must take a look at the following link which enables you to get files as per size: http://my.safaribooksonline.com/book/programming/python/0596001673/files/pythoncook-chp-4-sect-24 Basically it says, Get statistics on each item--file and subdirectory--of start where start can be root and traverse in sub-dir from then on.

fscore
  • 2,567
  • 7
  • 40
  • 74