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/')
Related
Hello I have a code to zip all folders it still works normally outside but when the path
'C:\Users\Dr Corn\AppData\Local\Temp'
It does not work again
Here is my code:
from posixpath import dirort
import zipfile
import os
path = r'C:\Users\Dr Corn\AppData\Local\Temp'
folders = os.listdir(path)
for folder in folders:
if os.path.isdir(folder):
print(folder)
with zipfile.zipfile(folder + '.zip', 'w', zipfile.zip_deflated) as newzip:
for dirpath, dires, files in os.walk(folder):
for files in files:
newzip.write(os.path.join(dirpath, file))
We look forward to your help, thank you for reading
The code shared in question has lot of case related issues as noted by #AvleMonke.
If attempt in code is to create a zip file for each directory in the path given then following is the modified version of code in question that works.
from posixpath import *
import zipfile
import os
path = r'C:\Test\Dr Corn\App Data\Local\Temp'
folders = os.listdir (path)
for folder in folders:
folder_full_path = path+'\\'+folder
if os.path.isdir (folder_full_path):
with zipfile.ZipFile (folder_full_path + '.zip', 'w', zipfile.ZIP_DEFLATED) as newzip:
for dirpath, dires, files in os.walk (folder_full_path):
for files in files:
newzip.write (os.path.join (dirpath, files))
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 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/
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 would like to find all the files in a directory and all sub-directories.
code used:
import os
import sys
path = "C:\\"
dirs = os.listdir(path)
filename = "C.txt"
FILE = open(filename, "w")
FILE.write(str(dirs))
FILE.close()
print dirs
The problem is - this code only lists files in directories, not sub-directories. What do I need to change in order to also list files in subdirectories?
To traverse a directory tree you want to use os.walk() for this.
Here's an example to get you started:
import os
searchdir = r'C:\root_dir' # traversal starts in this directory (the root)
for root, dirs, files in os.walk(searchdir):
for name in files:
(base, ext) = os.path.splitext(name) # split base and extension
print base, ext
which would give you access to the file names and the components.
You'll find the functions in the os and os.path module to be of great use for this sort of work.
This function will help you: os.path.walk() http://docs.python.org/library/os.path.html#os.path.walk