I have a directory that has about 3000 directories. Each of these 3000 directories has 2 .jpg images. My goes is to move each image from each of these 3000 directories into one separate folders. This is the master directory that has all the images.
Here is a more visual version of my structure:
-dir1
-littledir1
- image1.jpg
-image2.jpg
-dir2
-littledir2
-image1.jpg
-image2.jpg
.
.
.
-dir 3000
-litledir3000
-image1.jpg
-image2.jpg
I attempted to solve this problem with the following code without much success. The keeps running without copying over anything:
My code:
import os
import glob
import shutil
dirpath = '/Volumes/LaCie/lfw/'
dirs = os.listdir(dirpath)
dst_dir = '/Volumes/LaCie/lfw/dstn/'
i = 1
for dirname in dirs:
if (dirname.endswith('.DS_Store')):
continue
for jpgfile in glob.iglob(os.path.join(dirpath, "*.jpg")):
shutil.copy(jpgfile, dst_dir)
print (dst_dir)
How can I better approach this problem?
Did your print (dst_dir) print? I ran your code but that inner print did not run.
I'm not saying it's a better approach, but this worked for me...obviously you'd need to alter for your situation:
import glob
import os
import shutil
dirpath = 'dir1'
dirs = os.listdir(dirpath)
for dirname in dirs:
print(dirname)
os.path.join(dirpath)
# for file in glob.iglob(os.path.join(dirpath, '*.txt')):
# print(file)
for file in os.listdir(os.path.join(dirpath, dirname)):
shutil.copy(os.path.join(dirpath, dirname, file), '.')
print(file)
if you are on a *nix system use this
cp $(find dirpath -name "*jpg") dst_dir/
Related
I have a list of files like this in the images folder.
and How can I create a new folder if there are multiple files with a similar name and move those similar files to that folder?
I am new to python.
Here is my expectation:
Try this:
import glob
from pathlib import Path
for fn in Path("Images").glob("*"):
file_base_name = "_".join(fn.stem.split("_")[:-1])
file_count = len(glob.glob1("Images", f"{file_base_name}*"))
if file_count > 1 or Path(file_base_name).is_dir():
outdir = Path("Images") / file_base_name
outdir.mkdir(exist_ok=True)
fn.rename(outdir / fn.name)
Input:
Output:
Please ignore file names extension. I create those just to test my code
In this case you don't even need re:
from pathlib import Path
for fn in Path("Images").glob("*.jpg"):
outdir = Path("Images") / "_".join(fn.stem.split("_")[:-1])
outdir.mkdir(exist_ok=True)
fn.rename(outdir / fn.name)
What's going on here?
Pathlib is how you want to think of paths if you can. It combines most of the os.path apis. Specifically:
glob gets us all the files matching the glob in the path
mkdir makes the directory (only if it doesn't exist)
rename moves the file there
I am unable to test since I don't have your files. My suggestion would be to comment out the mkdir command and the shutil.move command and replace them with print statements to see what commands would be generated before letting it run for real. But I think it should work.
import pathlib
import os
import re
from itertools import groupby
import shutil
source_dir = 'Images'
files = [os.path.basename(f) for f in pathlib.Path(source_dir).glob('*.jpg')]
def keyfunc(file):
m = re.match('^(.*?)_\d+.jpg$', file)
return m[1]
matched_files = [file for file in files if re.search(r'_\d+.jpg$', file)]
matched_files.sort()
for k, g in groupby(matched_files, keyfunc):
new_dir = os.path.join(source_dir, k)
if not os.path.exists(new_dir):
os.mkdir(new_dir)
for file in g:
shutil.move(os.path.join(source_dir, file), new_dir)
I wrote down this code:
import shutil
files = os.listdir(path, path=None)
for d in os.listdir(path):
for f in files:
shutil.move(d+f, path)
I want every folder in a given directory (path) with files inside, the files contained in that folder are moved to the main directory(path) where the folder is contained.
For Example:
The files in this folder: C:/example/subfolder/
Will be moved in: C:/example/
(And the directory will be deleted.)
Sorry for my bad english :)
This should be what you are looking for, first we get all subfolders in our main folder. Then for each subfolder we get files contained inside and create our source path and destination path for shutil.move.
import os
import shutil
folder = r"<MAIN FOLDER>"
subfolders = [f.path for f in os.scandir(folder) if f.is_dir()]
for sub in subfolders:
for f in os.listdir(sub):
src = os.path.join(sub, f)
dst = os.path.join(folder, f)
shutil.move(src, dst)
Here another example , using a few lines with glob
import os
import shutil
import glob
inputs=glob.glob('D:\\my\\folder_with_sub\\*')
outputs='D:\\my\\folder_dest\\'
for f in inputs:
shutil.move(f, outputs)
I have a folder with many subfolders that contains images. I want to copy these images of the subfolders to the destination folder. All images should be in one folder. With my current code, Python copies all subfolders to the destination folder, but thats not what I want. I only what the .jpg images. My current code is:
dir_src = r"/path/to/folder/with/subfolders"
dir_dst = r"/path/to/destination"
for file in os.listdir(dir_src):
print(file)
src_file = os.path.join(dir_src, file)
dst_file = os.path.join(dir_dst, file)
shutil.copytree(src_file, dst_file)
I'm grateful for every tip
You can use os.walk:
import os
from shutil import copy
dir_src = r"/path/to/folder/with/subfolders"
dir_dst = r"/path/to/destination"
for root, _, files in os.walk(dir_src):
for file in files:
if file.endswith('.jpg'):
copy(os.path.join(root, file), dir_dst)
or you can use glob if you're using Python 3.5+:
import glob
from shutil import copy
dir_src = r"/path/to/folder/with/subfolders"
dir_dst = r"/path/to/destination"
for file in glob.iglob('%s/**/*.jpg' % dir_src, recursive=True):
copy(file, dir_dst)
I want to copy all my JPG files in one directory to a new directory.
How can I solve this in Python?I just start to learn Python.
Thanks for your reply.
Of course Python offers all the tools you need. To copy files, you can use shutil.copy(). To find all JPEG files in the source directory, you can use glob.iglob().
import glob
import shutil
import os
src_dir = "your/source/dir"
dst_dir = "your/destination/dir"
for jpgfile in glob.iglob(os.path.join(src_dir, "*.jpg")):
shutil.copy(jpgfile, dst_dir)
Note that this will overwrite all files with matching names in the destination directory.
import shutil
import os
for file in os.listdir(path):
if file.endswith(".jpg"):
src_dir = "your/source/dir"
dst_dir = "your/dest/dir"
shutil.move(src_dir,dst_dir)
Just use the following code
import shutil, os
files = ['file1.txt', 'file2.txt', 'file3.txt']
for f in files:
shutil.copy(f, 'dest_folder')
N.B.: You're in the current directory.
If You have a different directory, then add the path in the files list.
i.e:
files = ['/home/bucket/file1.txt', '/etc/bucket/file2.txt', '/var/bucket/file3.txt']
for jpgfile in glob.iglob(os.path.join(src_dir, "*", "*.jpg")):
shutil.copy(jpgfile, dst_dir)
You should write "**" before ".jpg" to search child directories. more "" means more subdirectory to search
I need to copy files with extension of .jpg from a folder tree.
The folder tree is like this:
-folder_A
-folder_1
-1.txt
-1.jpg
-folder_2
-2.txt
-2.jpg
-folder_3
-3.txt
-4.jpg
-folder_B
How can I copy all the x.jpg to folder_B?
i.e. All the files are the same in folder_A, and in folder_B, files are 1.jpg, 2.jpg...
Have a look at the python os module.
import os
import shutil as sh
root_path = "./folder_A"
dest_path = "./folder_B"
for dirpath, dnames, fnames in os.walk(root_path):
for f in fnames:
if f.endswith(".jpg"):
source_file_path = os.path.join(dirpath, f)
dest_file_path = os.path.join(dest_path, f)
sh.copyfile(source_file_path, dest_file_path)
Or if you know your OS you can just execute the according shell-command.
(But I think the solution of #stellasia is cleaner)
Example (Linux):
import os
os.system('cp -r folder_A/*/*.jpg folder_B/')