Advertisement
AceScottie

FolderScanner

Apr 25th, 2019
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. from aceutil import Log
  2. log = Log()
  3. def file_is_hidden(p): #checks if the file is  hidden file
  4.     p.replace("/", "\\")
  5.     try:
  6.         if os.name== 'nt':
  7.             attribute = win32api.GetFileAttributes(p) & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
  8.             if attribute > 0:
  9.                 attribute = True
  10.             else:
  11.                 attribute = False
  12.             return attribute
  13.         else:
  14.             return p.startswith('.')
  15.     except Exception as err:
  16.         exc_type, exc_obj, exc_tb = sys.exc_info()
  17.         log.error("sftp.file_is_hidden failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))
  18. def get_files_folders(location): #gets a list of files and a list of folders from a location
  19.     try:
  20.         files=[]
  21.         folders=[]
  22.         objects = [f for f in os.listdir(location) if not self.file_is_hidden(location+f)]
  23.         for i in range(len(objects)):
  24.             objects[i] = str(objects[i])
  25.         for i in objects:
  26.             fileattr = os.lstat(location+i)
  27.             if stat.S_ISDIR(fileattr.st_mode):
  28.                 folders.append(i)
  29.             if stat.S_ISREG(fileattr.st_mode):
  30.                 files.append(i)
  31.         return folders, files
  32.     except Exception as err:
  33.         exc_type, exc_obj, exc_tb = sys.exc_info()
  34.         log.error("sftp.get_files_folders failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))
  35.         return [], []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement