Advertisement
Poganu

Shuffle-Files.py

Apr 24th, 2022
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. ########################################
  2. #####Grab randomly data from folder#####
  3. ########################################
  4. #0. Import variables
  5. #0. Import links
  6. #1. Declare variables
  7. #2. Create "Shuffled" folder
  8. #3. Read folder files - place into list
  9. #4. If too many files wanted, exit
  10. #5. Declare empty random number list
  11. #6. Loop for evey element in the given number of shuffled files
  12. #7. Generate random number between 0 and end of list
  13. #8. If the element exists, try again and find a new one
  14. #9. If new random number generated, grab file with this number and copy to Shuffled folder
  15.  
  16. #0. Import variables
  17. import os
  18. import glob
  19. import random
  20. from sys import exit
  21.  
  22. #0. Import links
  23. source_folder = "/Users/cristianpogan/Downloads/Test-comparatie-frames/outputlit2/No-Bin-24-Apr/"
  24. destination_folder = f"{source_folder}Shuffled/"
  25.  
  26. #1. Declare variables
  27. wanted_files = 20
  28.  
  29. #2. Create "Shuffled" folder
  30. os.system(f"mkdir {destination_folder}")
  31.  
  32. #3. Read folder files - place into list
  33. all_files = glob.glob(f"{source_folder}*")
  34. print("list size: ", len(all_files))
  35.  
  36. #4. If too many files wanted, exit
  37. if wanted_files > len(all_files):
  38.     print("Cannot grab more than the number of files in folder")
  39.     exit()
  40.  
  41. for video in all_files:
  42.     print(video)
  43.  
  44. #5. Declare empty random number list
  45. rand_list = []
  46.  
  47. #6. Loop for evey element in the given number of shuffled files
  48. for file in range(wanted_files):
  49.     #7. Generate random number between 0 and end of list
  50.     rand = random.randint(0,len(all_files))
  51.     print(rand)
  52.  
  53.     #8. If the element exists, try again and find a new one
  54.     if (rand in rand_list):
  55.         print ("Element Exists")
  56.         file = file - 1
  57.         #Otherwise, it will shorten the loop with the elements already in the rand_list
  58.  
  59.     else:
  60.         #9. If new random number generated, grab file with this number and copy to Shuffled folder
  61.         print(f"Number {rand} not in list. Appending")
  62.         rand_list.append(rand)
  63.         print(f"Grabbing and copying element {all_files[file]}")
  64.         print("to ")
  65.         print(destination_folder)
  66.         os.system(f"cp {all_files[file]} {destination_folder}")
  67.        
  68.  
  69. print("List of random files: ")
  70. print(rand_list)
  71. ########################################
  72. #####Grab randomly data from folder#####
  73. ########################################
  74. #0. Import variables
  75. #0. Import links
  76. #1. Declare variables
  77. #2. Create "Shuffled" folder
  78. #3. Read folder files - place into list
  79. #4. If too many files wanted, exit
  80. #5. Declare empty random number list
  81. #6. Loop for evey element in the given number of shuffled files
  82. #7. Generate random number between 0 and end of list
  83. #8. If the element exists, try again and find a new one
  84. #9. If new random number generated, grab file with this number and copy to Shuffled folder
  85.  
  86. #0. Import variables
  87. import os
  88. import glob
  89. import random
  90. from sys import exit
  91.  
  92. #0. Import links
  93. source_folder = "/Users/cristianpogan/Downloads/Test-comparatie-frames/outputlit2/No-Bin-24-Apr/"
  94. destination_folder = f"{source_folder}Shuffled/"
  95.  
  96. #1. Declare variables
  97. wanted_files = 20
  98.  
  99. #2. Create "Shuffled" folder
  100. os.system(f"mkdir {destination_folder}")
  101.  
  102. #3. Read folder files - place into list
  103. all_files = glob.glob(f"{source_folder}*")
  104. print("list size: ", len(all_files))
  105.  
  106. #4. If too many files wanted, exit
  107. if wanted_files > len(all_files):
  108.     print("Cannot grab more than the number of files in folder")
  109.     exit()
  110.  
  111. for video in all_files:
  112.     print(video)
  113.  
  114. #5. Declare empty random number list
  115. rand_list = []
  116.  
  117. #6. Loop for evey element in the given number of shuffled files
  118. for file in range(wanted_files):
  119.     #7. Generate random number between 0 and end of list
  120.     rand = random.randint(0,len(all_files))
  121.     print(rand)
  122.  
  123.     #8. If the element exists, try again and find a new one
  124.     if (rand in rand_list):
  125.         print ("Element Exists")
  126.         file = file - 1
  127.         #Otherwise, it will shorten the loop with the elements already in the rand_list
  128.  
  129.     else:
  130.         #9. If new random number generated, grab file with this number and copy to Shuffled folder
  131.         print(f"Number {rand} not in list. Appending")
  132.         rand_list.append(rand)
  133.         print(f"Grabbing and copying element {all_files[file]}")
  134.         print("to ")
  135.         print(destination_folder)
  136.         os.system(f"cp {all_files[file]} {destination_folder}")
  137.        
  138.  
  139. print("List of random files: ")
  140. print(rand_list)
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement