Guest User

Untitled

a guest
Jul 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import os
  2.  
  3. class ScanFile(object):
  4. def __init__(self,directory,prefix=None,postfix=None):
  5. self.directory=directory
  6. self.prefix=prefix
  7. self.postfix=postfix
  8.  
  9. def scan_files(self):
  10. files_list=[]
  11.  
  12. for dirpath,dirnames,filenames in os.walk(self.directory):
  13. '''
  14. dirpath is a string, the path to the directory.
  15. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..').
  16. filenames is a list of the names of the non-directory files in dirpath.
  17. '''
  18. for special_file in filenames:
  19. if self.postfix:
  20. special_file.endswith(self.postfix)
  21. files_list.append(os.path.join(dirpath,special_file))
  22. elif self.prefix:
  23. special_file.startswith(self.prefix)
  24. files_list.append(os.path.join(dirpath,special_file))
  25. else:
  26. files_list.append(os.path.join(dirpath,special_file))
  27.  
  28. return files_list
  29.  
  30. def scan_subdir(self):
  31. subdir_list=[]
  32. for dirpath,dirnames,files in os.walk(self.directory):
  33. subdir_list.append(dirpath)
  34. return subdir_list
  35.  
  36. if __name__=="__main__":
  37. dir=r'C:\videos'
  38. scan=ScanFile(dir)
  39. subdirs=scan.scan_subdir()
  40. files=scan.scan_files()
Add Comment
Please, Sign In to add comment