加密文件,复制目录树和那些文件,然后全部复制到另一个目标位置

问题描述 投票:0回答:1

这是我所拥有的:

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

python encryption copy shutil os.path
1个回答
0
投票

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.