Шифровать файлы, копировать дерево каталогов и эти файлы и копировать все в другое место назначения - PullRequest
0 голосов
/ 12 апреля 2020

Вот что у меня есть:

bufferSize = 64 * 1024
password1 = 'password'

def copyDirectoryTree(source, dest, symlinks=False, ignore=None):
    if not os.path.exists(dest):
        os.makedirs(dest)
    for item in os.listdir(source):
        pathOrigen = os.path.join(source, item)
        pathDest = os.path.join(dest, item)
        if os.path.isdir(pathSource):
            copiarArbolDirectorios(pathSource, pathDest, symlinks, ignore)
        else:
            if not os.path.exists(pathDest) or os.stat(pathSource).st_mtime - 
os.stat(pathDest).st_mtime > 1:
            shutil.copy2(pathSource, pathDest)

copyDirectoryTree(sourcePath, destinationPath)

############# Encryptation #############
for archivo in glob.glob(destinationPath + '//**/*', recursive=True):
    fullPath = archivo
    fullNuevoFichero = archivo + '.aes'
    if os.path.isfile(fullPath):
        print('>>> Original: \t' + fullPath + '')
        print('>>> Encriptado: \t' + fullNuevoFichero + '\n')
        pyAesCrypt.encryptFile(fullPath, fullNuevoFichero, password1, bufferSize)
        os.remove(fullPath)

Код сначала копирует дерево с файлами и всем, а затем шифрует файлы и удаляет файлы с расширением, отличным от .aes, но я хотел бы копировать только файлы с расширением .aes шифрование перед копированием, но я не знаю, как действовать.

1 Ответ

0 голосов
/ 13 апреля 2020

Я исправил это. Теперь он перемещает файлы, когда они зашифрованы.

bufferSize = 64 * 1024
password1 = 'password'

def copyDirectoryTree(source, dest, symlinks=False, ignore=None):
    if not os.path.exists(dest):
        os.makedirs(dest)
    for item in os.listdir(source):
        pathOrigen = os.path.join(source, item)
        pathDestino = os.path.join(dest, item)
        if os.path.isdir(pathSource):
            copiarArbolDirectorios(pathAource, pathDest, symlinks, ignore)
        else:
            if not os.path.exists(pathDest) or os.stat(pathsource).st_mtime - os.stat(pathDest).st_mtime > 1:
                if item.endswith('.aes'):
                    shutil.copy2(path>ource, pathDest)

############# Encryptation #############
for archivo in glob.glob(sourcePath + '//**/*', recursive=True):
    fullPath = archivo
    fullNuevoFichero = archivo + '.aes'
    if os.path.isfile(fullPath):
        print('>>> Original: \t' + fullPath + '')
        print('>>> Encriptado: \t' + fullNuevoFichero + '\n')
        pyAesCrypt.encryptFile(fullPath, fullNuevoFichero, password1, bufferSize)
        copiarArbolDirectorios(sourcePath, destinationPath)
        os.remove(fullNuevoFichero)
...