Python Forum
Moving 2 photos from each subfolder to another folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving 2 photos from each subfolder to another folder
#1
Photo 
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)
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 1,605 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 2,164 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename multiple photos with DateTimeOriginal timestamp Stjude1982 2 3,207 Oct-21-2022, 12:24 AM
Last Post: Pedroski55
  how to extract tiff images from the subfolder into. hocr format in another similar su JOE 0 1,992 Feb-16-2022, 06:28 PM
Last Post: JOE
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 7,478 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Created zipfile without all the subfolder? korenron 3 7,437 Jun-23-2021, 12:44 PM
Last Post: korenron
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 3,628 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 7,825 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Delete directories in folder is not working after folder is updated asheru93 2 3,918 Feb-13-2019, 12:37 PM
Last Post: asheru93
  copy content of folder to existing folder shlomi27 0 3,344 Aug-11-2018, 01:44 PM
Last Post: shlomi27

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.