
import os

class Dirmuncher:
    def __init__(self, directory):
        self.directory = directory

    def directoryListing(self):
        for dirname, dirnames, filenames in os.walk(self.directory):
            # Subdirectories
            for subdirname in dirnames:
                print(os.path.join(dirname, subdirname))

            # Filenames
            for filename in filenames:
                print(os.path.join(dirname, filename))

if __name__ == "__main__":
    muncher = Dirmuncher('movies')
    muncher.directoryListing()


import os

class Dirmuncher:
    def __init__(self, directory):
        self.directory = directory

    def getFiles(self):
        result = {}

        for dirname, dirnames, filenames in os.walk(self.directory):
            # Subdirectories
            for subdirname in dirnames:
                print(os.path.join(dirname, subdirname))

            # Filenames
            for filename in filenames:
                print(os.path.join(dirname, filename))

            result[dirname] = filenames

        return result


if __name__ == "__main__":
    muncher = Dirmuncher('movies')
    print(muncher.getFiles())

Sort files into dict with dir as key
[py] Sort files into dict with dir as key

  
  import os
  
  class Dirmuncher:
      def __init__(self, directory):
          self.directory = directory
  
-     def directoryListing(self):
+     def getFiles(self):
+         result = {}
+ 
          for dirname, dirnames, filenames in os.walk(self.directory):
              # Subdirectories
              for subdirname in dirnames:
                  print(os.path.join(dirname, subdirname))
  
              # Filenames
              for filename in filenames:
                  print(os.path.join(dirname, filename))
  
+             result[dirname] = filenames
+ 
+         return result
+ 
+ 
  if __name__ == "__main__":
      muncher = Dirmuncher('movies')
-     muncher.directoryListing()
+     print(muncher.getFiles())
  
Sort files into dict with dir as key
## Code Before:

import os

class Dirmuncher:
    def __init__(self, directory):
        self.directory = directory

    def directoryListing(self):
        for dirname, dirnames, filenames in os.walk(self.directory):
            # Subdirectories
            for subdirname in dirnames:
                print(os.path.join(dirname, subdirname))

            # Filenames
            for filename in filenames:
                print(os.path.join(dirname, filename))

if __name__ == "__main__":
    muncher = Dirmuncher('movies')
    muncher.directoryListing()

## Instruction:
Sort files into dict with dir as key
## Code After:

import os

class Dirmuncher:
    def __init__(self, directory):
        self.directory = directory

    def getFiles(self):
        result = {}

        for dirname, dirnames, filenames in os.walk(self.directory):
            # Subdirectories
            for subdirname in dirnames:
                print(os.path.join(dirname, subdirname))

            # Filenames
            for filename in filenames:
                print(os.path.join(dirname, filename))

            result[dirname] = filenames

        return result


if __name__ == "__main__":
    muncher = Dirmuncher('movies')
    print(muncher.getFiles())
