0

I have a function which simply encrypts files inside a directory

def encrypt_directory(location):
    os.chdir(location)
    for file in glob.glob("*.*"):
        encrypt_file(file)
        print(file + " is encrypted")

My question is: How can i access to subdirectories of location and apply encrypt_file function to files inside them?

JayGatsby
  • 1,541
  • 6
  • 21
  • 41
  • 4
    `os.walk()` is what you need – Julien Spronck Mar 30 '15 at 19:58
  • 1
    possible duplicate of [Using os.walk() to recursively traverse directories in Python](http://stackoverflow.com/questions/16953842/using-os-walk-to-recursively-traverse-directories-in-python) – Scis Mar 30 '15 at 20:01

1 Answers1

1
def encrypt_directory(location):
    for root, _, files in os.walk(location):
        for fil in files:
            fname = os.path.join(root, fil)
            encrypt_file(fname)
            print(fname + " is encrypted")
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55