Bjarne K Bjarne K - 6 months ago 40
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):
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'


the error message:

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'

Answer

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)
Comments