Bjarne K Bjarne K - 5 months ago 23
Python Question

Getting files of every directory (Py)

I am programming a little script that encrypts every file, located after a specific path. In my case the scripts realpath...
in the first folder (script directory) works fine, but when I go to the next directory it tries to cd into the files located in the 2nd directory tier.
So the tree looks like [file, file, folder [file, file], file, file]

(I know, that the script and the key will be encrypted also, but I was to lazy for that yet... and sorry for my poor english, I hope you can understand me :P)

My code:

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):
f = open(filename, 'r')
fileString =
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
except IOError:
print '[!] File was a folder'
cryptFilesInFolder(currentDir + '/' + filename)

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

the error message:

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

File "crypt folder", line 11, in cryptFilesInFolder

File "crypt folder", line 25, in crypt

File "crypt folder", line 11, in cryptFilesInFolder

File "crypt folder", line 25, in crypt

File "crypt folder", line 6, in cryptFilesInFolder

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


It seems that you are trying to run a certain command on every file in a folder, including any files in (recursive) subfolders of that folder.

In that case, you want to use os.walk, which will recursively traverse the given directory and yield a tuple of the (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)

The map function simply applies crypt (well, a wrapper around crypt) to each item.

map(lambda f: crypt(f, key, root), files) is functionally equivalent to:

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