python - 加密文件,复制目录树和那些文件并将所有文件复制到另一个目的地
问题描述
这就是我所拥有的:
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的文件,这是在复制前加密,但我不知道如何进行。
解决方案
我修好了它。现在它会在文件被加密时移动文件。
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)
推荐阅读
- python - Python Numpy 将数组重塑为具有少于 m*n 个元素的 (m,n) 形状
- reactjs - setState 更新后 ReactJS 组件不重新渲染?
- email - 更改发件人的电子邮件地址
- c++ - 如何在提升循环缓冲区中一次访问所有元素?
- .net - 如何在 Azure 的 VM 上运行 .NET?
- wavesurfer.js - wavesurfer.js 尝试在区域中设置句柄样式
- asp.net-mvc - c# .net5 动态前缀url 使前缀因访问时搜索多次变化
- python - python _thread 返回第一个 arg 必须是可调用的
- angular - 如何在 Angular 服务中本地保存 Apollo 响应?
- android - Kotlin 捕获图像并将其存储在本地