Moving 2 photos from each subfolder to another folder - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Moving 2 photos from each subfolder to another folder (/thread-35398.html) |
Moving 2 photos from each subfolder to another folder - Blacklonewolf - Oct-28-2021 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 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) RE: Moving 2 photos from each subfolder to another folder - DeaD_EyE - Oct-28-2021 The old functions in os and os.path are very low level and are hard to use.The Path object gives you more Abstraction. import shutil from pathlib import Path def move_files(sources, destination, extensions=None): # converting the str to a Path object # if it's already a Path object, it # will return also a Path object destination = Path(destination) # you've more than one source directory # so iterating over this list with possible str or Path objects for source in sources: # here the same conversion to Path objects happens like before source = Path(source) # checking if it's on the same file system # if this is the case, the file can be renamed # otherwise the file must be copied same_filesystem = source.stat().st_dev == destination.stat().st_dev for source_path in source.iterdir(): # continue if source_path is not regular file if not source_path.is_file(): # continue -> back to the top of the inner for-loop continue # checking first if extension is provided # and if the suffix is in extensions # if the first condition is not True, the # second condition is not evaluated if extensions and source_path.suffix not in extensions: continue dst_path = destination / source_path.name # checking if the file on the target already exists # and skipping it if it's the case if dst_path.exists(): continue # if the file is on the same filesystem # then the file could be renamed # otherwise the slow copy operation must be used if same_filesystem: # rename operation on same fs source_path.rename(dst_path) else: # move operation if it's not the same fs # move does two operations: copy and then delete the # old file shutil.move(source_path, dst_path)For example, move_files(["A", "B", "C"], "Q") will rename/move files from directories A, B, C (one level deep) to directory Q. move_files(["A", "B", "C"], "Q", [".jpg", ".png"]) will move only jpg and png files.
|