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

时间:2016-05-23 15:17:11

标签: python pycrypto

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

(我知道,剧本和密钥也会被加密,但我还是懒得......而且对不起我的英语很差,我希望你能理解我: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'

1 个答案:

答案 0 :(得分:4)

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

在这种情况下,您希望使用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)