Advertisement
HoCo_xXSamXx

Functions

Nov 4th, 2023 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | Source Code | 0 0
  1. def ListFill():
  2.     # Find the maximum length among the lists in data
  3.     max_length = max(len(data[key]) for key in data)
  4.     # Fill lists to the maximum length with empty strings
  5.     for key in data:
  6.         data[key] += [''] * (max_length - len(data[key]))
  7.        
  8. def SelectionChecker(Selection):
  9.     if os.path.isfile(Selection): return "File"
  10.     elif os.path.islink(Selection): return "Link"
  11.     elif os.path.isdir(Selection): return "Dir"
  12.     else:
  13.         try:
  14.             # Use os.stat to access file metadata
  15.             stat_info = os.stat(path)
  16.             if stat_info.st_mode & 0o170000 == 0o020000: return "Character Device"
  17.             elif stat_info.st_mode & 0o170000 == 0o060000: return "Block Device"
  18.             else: return "Unknown"
  19.         except Exception as e:
  20.             return "Unknown"    
  21.    
  22. def SelectionFormat(SelectionName, SelectionType):
  23.     if SelectionName in RestrictedDir: FormatOutput         = SelectionName + "*Locked*"; return FormatOutput
  24.     if SelectionType == "File": FormatOutput                = SelectionName + ""; return FormatOutput
  25.     if SelectionType == "Link": FormatOutput          = "~" + SelectionName + "/"; return FormatOutput
  26.     if SelectionType == "Dir": FormatOutput                 = SelectionName + "/"; return FormatOutput
  27.     if SelectionType == "Character Device": FormatOutput    = SelectionName + "CDCD"; return FormatOutput
  28.     if SelectionType == "Block Device": FormatOutput        = SelectionName + "BDBD"; return FormatOutput
  29.     if SelectionType == "Unknown": FormatOutput             = SelectionName + "/"; return FormatOutput
  30.    
  31. def FileOpen(FileName):
  32.     if FileName == 0:
  33.         FileOutput = "~"
  34.         #return FileOutput
  35.     else:
  36.         os.chdir("..")
  37.         FileOutput = []
  38.         encodings_to_try = ["utf-8", "latin-1", "iso-8859-1", "windows-1252"]  # Add more encodings as needed
  39.  
  40.         for encoding in encodings_to_try:
  41.             try:
  42.                 with open(FileName, "r", encoding=encoding) as file:
  43.                     for line in file:
  44.                         FileOutput.append(line)
  45.                 break  # Stop trying encodings if successful
  46.             except UnicodeDecodeError as e:
  47.                 # Handle the decoding error, for example, by skipping the problematic line
  48.                 FileOutput.append(f"Error decoding with {encoding}: {str(e)}")
  49.  
  50.     return FileOutput  
  51.    
  52. def SelectionOpen(Selection):
  53.     SelectionName = Selection
  54.     if SelectionName == 0:
  55.         Output = "~"
  56.     elif SelectionChecker(SelectionName) == "File" or SelectionChecker(SelectionName) == "None":
  57.         Output = "Open Me"
  58.         FileOpen(SelectionName)
  59.     else:
  60.         try:
  61.             SelectionDir = os.listdir(SelectionName)
  62.             SelectionType = SelectionChecker(SelectionName)
  63.  
  64.             Output = []
  65.  
  66.             for SelectionName in SelectionDir:    
  67.                 SelectionType = SelectionChecker(SelectionName)
  68.                 SelectionOut = SelectionFormat(SelectionName, SelectionType)
  69.                 Output.append(SelectionOut)
  70.         except Exception as e:
  71.             SelectionName = SelectionName[:-1]
  72.             FileOpen(SelectionName)
  73.             Output = "Open Me!"
  74.     if not Output: Output = "Empty"
  75.     return Output
  76.    
  77.        
  78. "Functions"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement