首页 > 解决方案 > 加密文件,复制目录树和那些文件并将所有文件复制到另一个目的地

问题描述

这就是我所拥有的:

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的文件,这是在复制前加密,但我不知道如何进行。

标签: pythonencryptioncopyshutilos.path

解决方案


我修好了它。现在它会在文件被加密时移动文件。

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)

推荐阅读