python - 获取每个目录的文件(Py)

标签 python pycrypto

我正在编写一个小脚本来加密位于特定路径后的每个文件。在我的例子中,脚本 realpath ... 在第一个文件夹(脚本目录)中工作正常,但是当我转到下一个目录时,它会尝试 cd 进入位于第二目录层中的文件。 所以树看起来像 [file, file, folder [file, file], file, file]

(我知道,脚本和 key 也将被加密,但我对此太懒了......抱歉我的英语不好,我希望你能理解我:P)

我的代码:

import os
import Crypto
from Crypto.PublicKey import RSA

def cryptFilesInFolder(currentDir):
    content_list = os.listdir(currentDir)
    print content_list
    print '[+] Start encrypting files in Dir: ' + currentDir
    for filename in content_list:
        print '[+] Encrypting ' + filename
        crypt(filename, key, currentDir)

def crypt(filename, key, currentDir):
    try:
        f = open(filename, 'r')
        fileString = f.read()
        f.close()
        print '[+] Encrypting file: ' + filename + ' with 4096 bytes'
        encryptedFileString = key.publickey().encrypt(fileString, 4096)
        f = open (filename, 'w')
        f.write(str(encryptedFileString)) #write ciphertext to file
        f.close()
    except IOError:
        print '[!] File was a folder'
        cryptFilesInFolder(currentDir + '/' + filename)

print '[+] Startet Crypting'
print '[+] Reading Key'
f = open('mykey.pem','r')
key = RSA.importKey(f.read())
f.close()
print '[+] Key imported'
print '[+] Setting Root Directory'
rootDir = os.path.realpath(__file__)
print 'Root Directory set'
print '[+] Starting encryption in folder: '
cryptFilesInFolder(os.path.dirname(os.path.realpath(__file__)))
print '[+] Finished \n\n\n'

错误信息:

Bjarne-2:crypt bjarne$ python crypt\ folder\ Kopie.py 
[+] Startet Crypting
[+] Reading Key
[+] Key imported
[+] Setting Root Directory
Root Directory set
[+] Starting encryption in folder: 
['.DS_Store', 'crypt folder Kopie.py', 'myKey.pem', 'Neuer Ordner']
[+] Start encrypting files in Dir: /Users/bjarne/Desktop/crypt
[+] Encrypting .DS_Store
[+] Encrypting file: .DS_Store with 4096 bytes
[+] Encrypting crypt folder Kopie.py
[+] Encrypting file: crypt folder Kopie.py with 4096 bytes
[+] Encrypting myKey.pem
[+] Encrypting file: myKey.pem with 4096 bytes
[+] Encrypting Neuer Ordner
[!] File was a folder
['.DS_Store', 'key Kopie.py']
[+] Start encrypting files in Dir: /Users/bjarne/Desktop/crypt/Neuer Ordner
[+] Encrypting .DS_Store
[+] Encrypting file: .DS_Store with 4096 bytes
[+] Encrypting key Kopie.py
[!] File was a folder
Traceback (most recent call last):
  File "crypt folder Kopie.py", line 37, in <module>

  File "crypt folder Kopie.py", line 11, in cryptFilesInFolder

  File "crypt folder Kopie.py", line 25, in crypt

  File "crypt folder Kopie.py", line 11, in cryptFilesInFolder

  File "crypt folder Kopie.py", line 25, in crypt

  File "crypt folder Kopie.py", line 6, in cryptFilesInFolder

OSError: [Errno 20] Not a directory: '/Users/bjarne/Desktop/crypt/Neuer Ordner/key Kopie.py'

最佳答案

您似乎正试图对文件夹中的每个文件运行特定命令,包括该文件夹(递归)子文件夹中的所有文件。

在这种情况下,您要使用 os.walk ,这将递归地遍历给定的目录并产生一个 (current directory, directories, files) 的元组。

import os
for (root, dirs, files) in os.walk(rootDir):
    # In each iteration, files will contain the list of files in the directory,
    # where directories are traversed recursively.
    map(lambda f: crypt(f, key, root), files)

map函数简单地将 crypt(嗯,crypt 的包装器)应用于每个项目。

map(lambda f: crypt(f, key, root), files) 在功能上等同于:

for f in files:
    crypt(f, key, root)

关于python - 获取每个目录的文件(Py),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394705/

相关文章:

python - 使用 PyCrypto AES 256 加密和解密

python - 无法在 Windows virtualenv 上安装 pycrypto

python - Pip 错误 : Microsoft Visual C++ 14. 需要 0

Java-Python AES加密解密

python : How to create mysql databases with PyMySQL?

python - 获取列表的每一部分

python - 如何从本地系统在远程服务器上运行 Flask 应用程序?

python - 如何为 QListWidget 设置模型

python - 为什么我可以使用一个 DES key 加密数据并使用另一个成功解密?

python - 在 Python 中查找函数 x = a*sin(x) 的所有根