Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda