Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

this is an example how to copy a file to USB(removable device) you can do invert.

import os
file = "cat.jpg"
os.system("for /F \"tokens=1*\" %a in (\'fsutil fsinfo drives\') do (for %c in (%b) do (for /F \"tokens=3\" %d in (\'fsutil fsinfo drivetype %c\') do (if %d equ Removable (copy " + file + " %c))))")
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda