1

Ubuntu user here. I am looking for a way to copy all flash drive and phone/tablet data to a specified folder whenever they are inserted. The python code below works great for flash drives but for some reason it will not work on my android phone or tablet. Additionally, I would like to only copy picture and video file extensions. My intention is to come home after a day of filming and plug in all my devices and have the pictures and video copy over. Thanks!

import subprocess
import time
import shutil

    #--
    target_folder = "/path/to/target_folder"
    excluded = ["media_extern"]
    #--

    def get_mountedlist():
        return [(item.split()[0].replace("├─", "").replace("└─", ""),
                 item[item.find("/"):]) for item in subprocess.check_output(
                ["/bin/bash", "-c", "lsblk"]).decode("utf-8").split("\n") if "/" in item]

    def identify(disk):
        command = "find /dev/disk -ls | grep /"+disk
        output = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
        if "usb" in output:
            return True
        else:
            return False

    done = []
    while True:
        mounted = get_mountedlist()
        new_paths = [dev for dev in get_mountedlist() if not dev in done and not dev[1] == "/"]
        valid = [dev for dev in new_paths if (identify(dev[0]), dev[1].split("/")[-1]  in excluded) == (True, False)]
        for item in valid:
            target = target_folder+"/"+item[1].split("/")[-1]
            try:
                shutil.rmtree(target)
            except FileNotFoundError:
                pass
            shutil.copytree(item[1], target)
        done = mounted
        time.sleep(4)here
user989805
  • 31
  • 6
  • Most modern androids tablets/phones, if not all i know of don't use USB Mass storage Mode, instead they use MTP(Media Transfer Protocol) or PTP (Picture Transfer protocol) – danidee Feb 11 '16 at 03:34
  • Thanks for reminding me - my friend said something along those lines, but he wasnt sure how to apply that to the above code. – user989805 Feb 11 '16 at 03:39
  • This might be of help http://stackoverflow.com/questions/11161747/how-to-access-an-mtp-usb-device-with-python a quick google search also brought out some python-wrappers around libmtp, be careful though (mtp on ubuntu dosen't work as well as windows for me) and some of the libraries are still buggy – danidee Feb 11 '16 at 03:59

0 Answers0