|
|
|
|
Python 3
|
28 lines
|
22 sloc
|
0.903 kB
  1. #!/usr/bin/env python3 
  2. import os 
  3. import sys 
  4. import shutil 
  5.  
  6. def main(): 
  7.     print("If folder name matches file, move it up a directory and delete the folder") 
  8.     path = os.path.realpath(sys.argv[1]) 
  9.     if not (os.path.isdir(path)): 
  10.         print(path + " is not a path!") 
  11.         sys.exit(4) 
  12.  
  13.     print("Using " + path) 
  14.     for subdir in get_immediate_subdirectories(path): 
  15.         print("Found " + os.path.realpath(subdir)) 
  16.         for f in os.listdir(subdir): 
  17.             if (os.path.basename(f) == os.path.basename(subdir)): 
  18.                 print("MATCH " + f + " " + subdir) 
  19.                 shutil.move(os.path.realpath(f), os.path.dirname(os.path.realpath(subdir))) 
  20.  
  21.  
  22. def get_immediate_subdirectories(a_dir): 
  23.     # https://stackoverflow.com/a/800201/1709894 
  24.     return [name for name in os.listdir(a_dir) 
  25.             if os.path.isdir(os.path.join(a_dir, name))] 
  26.  
  27.  
  28. main()