collinsanele

My searcher (search for music, images etc)

Dec 13th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import os
  2.  
  3. if "/" in os.getcwd():
  4.     for item in os.path.abspath(os.getcwd()).split("/"):
  5.         if item:
  6.             p = item
  7.             break
  8.            
  9. elif "\\" in os.getcwd():
  10.     for item in os.path.abspath(os.getcwd()).split("\\"):
  11.         if item:
  12.             p = item
  13.             break
  14.    
  15. if p == "storage":
  16.     p = "/"+str(p)
  17.    
  18.  
  19.        
  20.  
  21.  
  22.  
  23. def my_searcher(path_=p):
  24.     while True:
  25.         #tsize = 0
  26.         c = 0
  27.    
  28.         ask = str(input("\n\nEnter 0 to search for an mp3 or mp4 by name\nEnter 1 to search for all music(mp3) files\n2 for all mp4 files\n3 for all documents pdf files\n4 for all images jpg, jpeg files\n5 for all txt files\n6 for all docx files\n7 for all py (python) files\n8 for gif files\n9 for all xlsx, csv files\nq to exit\n\n"))
  29.    
  30.         print(50*"#")
  31.         info = {"1":("mp3"), "2":("mp4"),
  32.     "3":("pdf"),
  33.     "4":("jpeg", "jpg"),
  34.     "5":("txt"), "6":"docx", "7":("py"),
  35.     "8":"gif", "9":("xlsx", "csv")}
  36.    
  37.    
  38.         if ask == "q":
  39.             print("Bye")
  40.             break
  41.            
  42.         elif ask == "0":
  43.             name = input("Enter the name or some part of the name of the music or video to search\n\n")
  44.            
  45.             for path, dirs, files in os.walk(path_):
  46.                 for file in files:
  47.                     if file.endswith(("mp3", "mp4", "avi", "gif")):
  48.                         if name.lower() in file.lower():
  49.                             c+=1
  50.                             size = os.path.getsize(os.path.join(path,file))
  51.                             print(f"{file} --> {size/10**6}MB  ({os.path.join(path, file)})\n")
  52.             print(f"Total of {c} results found")
  53.            
  54.         else:
  55.             helper(path_=path_, target=info.get(ask))
  56.                    
  57.             #print(f"{c} {info.get(ask)} files found with a total size of {tsize}MB")
  58.  
  59.  
  60. def helper(path_, target):
  61.     c = 0
  62.     tsize = 0
  63.     for path, dirs, files in os.walk(path_):
  64.             for file in files:
  65.                 if file.endswith(target):
  66.                
  67.                     c+=1
  68.                     size = os.path.getsize(os.path.join(path,file))
  69.                     tsize+=size
  70.                     print(f"{file} --> {size/10**6}MB  ({os.path.join(path, file)})\n")
  71.     print(f"Total of {c} results found")
  72.     print(f"Total size = {tsize/10e6}MB")                              
  73.                                                                
  74. pmemory = "/storage/emulated/0"
  75. mcard = "/storage"                                         
  76.                                                                                                
  77. if __name__ == "__main__":             
  78.     my_searcher()
Advertisement
Add Comment
Please, Sign In to add comment