Python - How to copy content of USB(Flash) to system directory

13,667

Solution 1

I have fix this and solved, hope this will help to those guys who are beginners like me.

First i code to copy files and directories using shutil for more help https://docs.python.org/2/library/shutil.html, connect usb drive to perform operation

Step 1 : code_to_copy.py

import os
import datetime
import shutil
from shutil import copytree, ignore_patterns

files = os.listdir('/media/user/')

destination = '/home/user/Path/Backup/back_%s'%datetime.datetime.now()
try :
    for f in files:
        source = '/media/user/%s' % f
        copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))    
except Exception as e:
    print e

you can run this file to check whether it is copying or not, then

create bash file

Step 2 : code_to_copy.sh

#!/bin/bash
python /home/user/path/code_to_copy.py

make sure you have permissions to this files

step 3 : add to cron

$ crontab -e

#add this line
* * * * * /home/user/path/code_to_copy.sh > /tmp/code_to_copy.log

# check log file at /tmp/code_to_copy.log

after a minute your connected device data will be copied to your backup folder and hola.

Solution 2

You can use pythons shutil library which is pretty simple to handle copy operations.If you need to automate the process try the below steps:

1:If getting all files in a pendrive or any other removable drive get all the files and write it into a list by following way:

import os
files = os.listdir('path-to-removable-media') 

2:once thats done iterate through list and use shutil library for copying files.

 import shutil
 for f in files:
     shutil.copyfile('/source path/%s' % f, '/destination path/%s' %f)

3:Now if you need to automate the process create a bash file(file with extension sh) with following content in your folder where python script is present.(ex:create file test.sh and copy following)

 #!/bin/bash
 clear
 python script.py 

4:Then add this in your cronjob if you need to check or run in specificintervals and if removable media is not connected exception case need to be also handled.

5:For getting source you can make use of subprocess in python.

import subprocess
output = subprocess.Popen("lsblk", stdout=subprocess.PIPE, shell=True)
for out in output.communicate()[0].split():
    if '/media/' in out:
        print out

This will give path of removable media in linux devices.

Share:
13,667
Kamlesh
Author by

Kamlesh

Lead Data Science Engineer : A results-driven, customer-focused, articulate, and analytical Software Engineer who can think out of the box. Strong in design and integration problem solving skills. Expert in Python with database analysis and design. Skilled in developing business plans, requirements specifications and User documentation. Strong written and verbal communications. Interested in a challenging technical track career in a development environment.

Updated on June 22, 2022

Comments

  • Kamlesh
    Kamlesh almost 2 years

    What i want to program is,

    When USB drive is connected to system the code must initiate automatically and Copy the content(Directories, Files etc.) of usb drive to Default Backup Directory of System.

    I have came across some sites and found that I can use shutil library https://docs.python.org/2/library/shutil.html High-level file operations. I haven't used Shutil Library, So is there any other way to achieve,

    has anyone did this before so please help. Thanks

  • Kamlesh
    Kamlesh about 8 years
    Hey @vishnu This helps me but how to get source path of flash drive?
  • Vishnu
    Vishnu about 8 years
    @KamleshJaiswal i have anwser for detecting remavable media
  • rfii
    rfii almost 4 years
    how are you ensuring that the USB drives are always mounted to /media/user/
  • rfii
    rfii almost 4 years
    But this method of detecting the removable media requires someone to look at the printed lines. How can the print result be taken automatically?
  • rfii
    rfii almost 4 years
    Can you explain what all the stuff in this really long line of code does?