PYTHON - Wait for completion of a copy of files before executing the next steps

Asked

Viewed 23 times

0

Hello I am creating a backup routine that consists of copying a series of files from one folder to another. After copying the file I want to run a getsize to recover the size of the directory. The problem is that getsize runs before the copy is finished. How do I have each step run only at the end of the previous?

import os
from datetime import date

today = str(date.today()).replace("-","")
todayformated = date.today().strftime("%d/%m/%Y")
source = "D:\\Origem\\"
destination = "D:\\Destino\\"
sourcebkpini = "D:\\backup.ini"
backupini = """[BACKUP]
DataBackup="""+todayformated+"""
DirDestinoBackup=
"""

def execBackup():
    try:
        os.popen('mkdir '+ destination+today)
        os.popen('copy ' + source + '* ' + destination+today, 'w')
        with open(sourcebkpini, 'w') as ini:
            ini.write(backupini)
            print('Calculando tamanho do diretório', os.path.getsize(destination+today))
    except:
        print("Erro")

execBackup()


retorno:

Calculando tamanho do diretório 4096
D:\Origem\imovel.DB
D:\Origem\imovel.MB
D:\Origem\imovel.PX
D:\Origem\imovel.XG0
  • Change os.popen for subprocess.getoutput.

  • A more pleasant solution that will work well in any operating system is to use the shutil.copyfile() rather than directly calling the operating system commands.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.