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

查看:2069
本文介绍了使用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子进程中指定7zip的路径与cmd变量分开,并使用变量而不是固定字符串构建命令:

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命令一部分调用的应用程序分开(这是可执行路径) ),并且需要将命令字符串构建为var可以处理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天全站免登陆