Home > python, ubuntu > Extract files from a directory to another by file extensions

Extract files from a directory to another by file extensions

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 script.py in the directory you would like to scan. Run the script on the terminal by:

python script.py “/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 = '.'
    try:
        opts, args = getopt.getopt(argv,"")
    except getopt.GetoptError:
        print 'script.py "path"'
        sys.exit(2)
    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__":
    main(sys.argv[1:])

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.

Advertisements
Categories: python, ubuntu Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: