Hello
Let me start by saying that I'm beginner in Python, so I'm hoping for ready/big help
I have a problem with the following code, which I need to rewrite in such a way that the number of files I select from EACH subfolder in the selected folder is moved/copied to the folder I select. Currently it flips me a certain number of files, but only selects two images total, and I want it to select 2 from each folder.
Thanks for help in advance
Code
Let me start by saying that I'm beginner in Python, so I'm hoping for ready/big help
I have a problem with the following code, which I need to rewrite in such a way that the number of files I select from EACH subfolder in the selected folder is moved/copied to the folder I select. Currently it flips me a certain number of files, but only selects two images total, and I want it to select 2 from each folder.
Thanks for help in advance
Code
import os
import random
import shutil
files_list = []
dirs_list = []
files_from_dir = []
for root, dirs, files in os.walk("E:\Wallpapers2"):
for dir in dirs:
dirs_list.append(os.path.join(root, dir))
for test in dirs_list:
for root, dirs, filess in os.walk(test):
for file2 in filess:
if file2.endswith(".jpg") or file2.endswith(".png") or file2.endswith(".jpeg"):
files_list.append(os.path.join(root, file2))
for test in dirs_list:
for root, dirs, filess in os.walk(test):
for file2 in filess:
if file2.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
files_from_dir.append(os.path.join(root, file2))
files_list.append(random.sample(files_from_dir, 2))
#print images
#lets me count and print the amount of jpeg,jpg,pmg
file_count = len(files_list)
print(file_count)
# print files_list
filesToCopy = random.sample(files_list, 2) #prints two random files from list
destPath = "E:\Wallpapers3"
# if destination dir does not exists, create it
if os.path.isdir(destPath) == False:
os.makedirs(destPath)
# iteraate over all random files and move them
for file in filesToCopy:
shutil.move(file, destPath)
