Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Copy a file every n seconds in python

With this code, I look at a source directory and copy it to the destination. But when I add a new file to the source directory, the copying is not done. And I want a file to be copied every second. This code is doing batch copying. Can you help me solve these two problems?

import os,os.path
import shutil
from os import listdir
from os.path import isfile, join

my_path = 'source_path'
target = 'target_path'


onlyfiles = [f for f in listdir(my_path ) if isfile(join(my_path , f))]

while True:
    for file in onlyfiles:
        if file.endswith(".gz"):           
            a = my_path + file
            b = target + file
            shutil.copy(a, b)
    time.sleep(1)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ultimately, this solution can make it work

import shutil
import time
from pathlib import Path
import glob

my_path = 'source_path'
target = 'target_path'

onlyfiles = glob.glob(my_path+'/*.gz')

for file in onlyfiles:
    shutil.copy(file,  target + '/'+ Path(file).name)
    time.sleep(1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading