使用python加密文件夹或zip文件 [英] Encrypt folder or zip file using python

查看:51
本文介绍了使用python加密文件夹或zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用 python 加密一个目录,但我不确定最好的方法是什么.我可以轻松地将文件夹转换为 zip 文件,但是从那里我尝试查找如何使用 AES 对其进行加密,但无法使其正常工作,我还尝试使用 7zip 加密来存档文件夹,但是也无法让它发挥作用,所以如果有人有另一种加密目录的解决方案,或者可以为我指出正确的方向,了解如何使用以前的方法之一,这将是有帮助的.(如果这有任何意义,我在 Windows 上)

So I am trying to encrypt a directory using python and I'm not sure what the best way to do that is. I am easily able to turn the folder into a zip file, but from there I have tried looking up how to encrypt it with AES, but couldn't get that to work and I have also tried encrypting using 7zip to archive the folder, but also couldn't get that to work, so if anybody has another solution to encrypt a directory or could point me in the right direction on how to use one of the previous methods that would be helpful. (I'm on windows if that has any significance)

推荐答案

通过子流程模块使用 7-Zip 有效.以下是我遇到并必须解决的一些问题:您需要在 Popen 子进程中与 cmd 变量分开指定 7zip 的路径,并使用变量而不是实心字符串构建命令:

Using 7-Zip through the subprocess module works. Here are some issues I encountered and had to resolve: You need to specify the path to 7zip separate from the cmd variable in the Popen subprocess, and build the command with variables rather than a solid string:

appPath="C:Program Files\7-Zip"
zApp="7z.exe"
zAction='a'
zPass='-pPASSWORD'
zAnswer='-y'
zDir=directoryToZip
progDir=os.path.join(appPath,zApp)

cmd = [zApp, zAction, zipFileName, zPass, zAnswer, zDir]
subprocess.Popen(cmd, executable=progDir, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

这将创建一个 zip 文件(在 zipFileName 变量中具有名称的位置),包括directoryToZip"路径内的内容(目录和文件)

That will create a zip file (in the location with the name in the zipFileName variable), including the contents (directories and files) inside the "directoryToZip" path

progDir 必须与您作为 Open 命令的一部分调用的应用程序分开指定(这是可执行路径),并且命令字符串需要构建为变量以处理 Windows 反斜杠转义设置.

progDir has to be specified for separate from the application you are calling as part of the Open command (this is the executable path), and the command string needed to be built out as variables to deal with the windows backslash escaping setup.

这篇关于使用python加密文件夹或zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆