Posts Tagged ‘python script’

Extract files from a directory to another by file extensions

May 19th, 2013 No comments

Due to the complexity of the folders created for testdisk, I decided to implement a script to get the data I am interest in.
Copy this script and save it as in the directory you would like to scan. Run the script on the terminal by:

python “/home/username/path-to-folder”

The argument is the path where you are going to save your extracted files.

import os
import shutil
import sys, getopt
from os.path import join, getsize

def main(argv):
    path = '.'
        opts, args = getopt.getopt(argv,"")
    except getopt.GetoptError:
        print ' "path"'
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith((".jpg",".mov",".mp4")) and getsize(join(root,name))/1024 > 100:
                print name, getsize(join(root,name))/1024
                shutil.copy2(join(root,name), args[0])

if __name__ == "__main__":

Once finished you will have in the folder you specified all the files greater than 100Kb and with extensions “.jpg”,”.mov” and “.mp4”. Feel free to add the extensions you want.

Categories: python, ubuntu Tags: , , ,