organise-videos.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import os
  2. import shutil
  3. # Find the files in the current directory.
  4. current_dir_files = os.listdir()
  5. # Find the path of the current directory.
  6. current_dir_path = os.getcwd()
  7. # The Instagram videos are in MP4 format.
  8. video_format = ['mp4']
  9. # Find the MP4 files and save them in a variable called 'filenames'.
  10. filenames = [filename for filename in current_dir_files if filename.split('.')[-1] in video_format]
  11. # We want a dictionary where the filenames are the keys and the usernames are the values.
  12. filenames_to_usernames = {}
  13. # Populate the dictionary by going through each filename and removing the undesired characters, leaving just the usernames.
  14. for filename in filenames:
  15. filename_parts = filename.split('_')[1:-3]
  16. usernames = '_'.join(filename_parts)
  17. filenames_to_usernames[filename] = usernames
  18. # The usernames are the values of the filenames_to_usernames dictionary.
  19. usernames = filenames_to_usernames.values()
  20. # Make a folder for each username.
  21. for username in usernames:
  22. if not os.path.isdir(username):
  23. os.mkdir(username)
  24. # Move the videos into the folders
  25. for filename, username in filenames_to_usernames.items():
  26. shutil.move(filename, username)
  27. num_videos_moved = len(filenames_to_usernames.keys())
  28. print("{} videos moved successfully.".format(num_videos_moved))