0

I would like to iterate through files in a specified directory which are bigger then 100 kb and end with a *.zip.

How can this be done in a efficient way?

Will go through any zip files but not necessarily files which are bigger than 100kb;

for i in os.listdir(os.getcwd()):
    if i.endswith(".zip"):
        ##print i
        continue
    else:
        continue

How can I incoprporate it in the if conditions? ie (if i.endswith(".zip") and >100kb). How could I used this file as an argument with myOtherPythonScript.py?

3kstc
  • 1,871
  • 3
  • 29
  • 53

2 Answers2

1

You could try something like this...

for i in os.listdir(os.getcwd()):
    if i.endswith(".zip"):
        if os.path.getsize(i) > 10240:
            print i
    continue
else:
    continue
Lance
  • 86
  • 4
  • how would one use the file or `i`, as an argument with a called python script within that if statement? ie after the `print i` how would I used the`i` as an argument to `myOtherPythonScript.py`. – 3kstc Aug 15 '16 at 04:02
  • You would need to make this a class that return I and import the class in myotherpythonscript.py – Lance Sep 19 '16 at 21:45
0

endswith and os.path.getsize are the two functions you want.

import os

file_names = [os.path.join(path, file_name) for file_name in os.listdir(path)]
for file_name in file_names:
    if file_name.endswith('zip') and os.path.getsize(file_name) >= 100*1024:
        pass
    else:
        pass
Batman
  • 8,571
  • 7
  • 41
  • 80