Python's shutil Not copying Files to Specified Folder - python

I am trying to move files from one folder to another folder within the same directory buy am coming across a problem.
This is what my code looks like thus far:
current_dir = os.path.dirname(os.path.realpath(__file__))
folders = get_all_folders(current_dir)
os.mkdir('FINAL') # Final output stored here
for folder in folders:
img_list = list(os.listdir(current_dir))
for img in img_list:
img_path = os.path.join(current_dir, img)
final_folder = os.path.join(current_dir, 'FINAL')
shutil.copyfile(img_path, final_folder)
The FINAL folder is created as intended, however instad of copying the imgs over to that folder, a file called FINAL is created in each directory I am looping through.
Any ideas on how I can solve this?

I think the mistake you are doing here is that you are trying to find the images in the current directory instead of folders in the current directory. So, when you call shutil.copyfile(), you should ideally get a IsADirectoryError as per the code provided by you. Anyways, I guess this should work:
current_dir = os.path.dirname(os.path.realpath(__file__))
# Get all folder names in current directory before making the "FINAL" folder.
folders = get_all_folders(current_dir)
os.mkdir('FINAL') # Final output stored here
for folder in folders:
folder_path = os.path.join(current_dir, folder)
img_list = list(os.listdir(folder_path))
for img in img_list:
img_path = os.path.join(folder_path, img)
final_folder = os.path.join(current_dir, 'FINAL')
shutil.copyfile(img_path, final_folder)
Also, you need to remove the FINAL folder each time you run this script. Better make such checks in the code itself.

Related

Saving the file downloaded with the pytube library to the desired location

For some reason I don't understand, the codes do not work, strangely, I do not get an error, it just does not save where I want, it saves to the folder where the file is run.
af = input("Link:")
yt = YouTube(af, on_progress_callback=progress_callback)
stream = yt.streams.get_highest_resolution()
print(f"Downloading video to '{stream.default_filename}'")
pbar = tqdm(total=stream.filesize, unit="bytes")
path = stream.download()
#Download path
b = open("\Download", "w")
b.write(stream.download())
b.close()
pbar.close()
print(Fore.LIGHTGREEN_EX+"Saved video to {}".format(path))
time.sleep(10)
First, to save the file in a specific directory we need to create the directory, I will do this through this function
import sys, os
def createDirectory(name):
path = sys.path[0] # Take the complete directory to the location where the code is, you can substitute it to the place where you want to save the video.
if not(os.path.isdir(f'{path}/{name}')): # Check if directory not exist
path = os.path.join(sys.path[0], name) # Set the directory to the specified path and the name that was given to it
os.mkdir(path) # Create a directory
After that we need to save the file in the newly created directory, for that within the "stream.download" in the parameter "output_path" we pass the total path to save and then the variable that contains the name of the newly created directory.
directoyName = 'Video'
createDirectory(directoyName)
path = sys.path[0] # Take the complete directory to the location where the code is, you can substitute it to the place where you want to save the video.
stream.download(output_path=f'{path}/{directoyName}') # Save the video in the newly created directory.
This way the file will be saved in the directory with the specified name.

How to save processed images to different folders within a folder in python?

I have code with looks through a folder 'Images' and then subfolders and processes all those images.
I now need to save those images to a parallel directory, i.e. a folder called 'Processed Images' (in same directory as 'Images' folder) and then to the subfolders within this folder - these subfolders are named the same as the subfolders in 'Images' - the image should save to the same name of subfolder that it came from.
I can get the images to save to 'Processed Images' but not the subfolders within it.
path = ("...\\Images")
for dirName, subdirList, fileList, in os.walk(path):
for file in fileList:
full_file_path = os.path.join(dirName, file)
if file.endswith((".jpg")):
image_file = Image.open(full_file_path)
image_file = image_file.convert('L')
image_file = PIL.ImageOps.invert(image_file)
image_file = image_file.resize((28, 28))
new_filename = file.split('.jpg')[0] + 'new.png'
path2 = ("...\\Processed Images")
image_file.save(os.path.join(path2, new_filename))
else: continue
You can use the function os.mkdir() to create a new folder. dirName returned by os.walk() gives you the current folder path, so you can just extract the part of the path that you need, append it to ...\\Processed Images and create the new folder if needed.
Be sure to use two separate folder trees for the input files and output files. Otherwise os.walk() will find the new directories with the output images and continue to iterate over them.
I think you can seriously simplify this code using pathlib. I’m not sure about the triple dots (I think they should be double) in your base paths but they may work for you.
from pathlib import Path
path = Path("...\\Images")
path2 = Path("...\\Processed Images")
path2.mkdir(exist_ok=True)
for jpg_file in p.glob('**/*.jpg'):
full_file_path = str(jpg_file)
image_file = Image.open(full_file_path)
image_file = image_file.convert('L')
image_file = PIL.ImageOps.invert(image_file)
image_file = image_file.resize((28, 28))
new_filename = jpg_file.stem + 'new.png'
image_file.save(str(path2 / new_filename))

How to export images into subdirectory in GIMP?

I use GIMP for some batch editing, part of this is exporting painting with the filename taken from original image "filename" with this code:
pdb.file_png_save_defaults(ima, drawable, fullpath, filename)
At this moment, I have
fullpath=filename
so it saves the image into same folder as the source and filename and fullpath are identical.
What I want to do is to put it into subdirectory in this folder, lets call it "subfolder". But if I try:
fullpath = '\subfolder\'+filename
I get an error, obviously, because I am working with Python (or any programming language) for like half an hour and I hardly know what I am doing. Does anybody know how to achieve exporting images into a subfolder?
UPDATE:
Now it looks like this
sourcename = pdb.gimp_image_get_filename(ima)
basedir = os.path.dirname(sourcename)
if os.path.isdir(os.path.join(basedir,'Subfolder')) is False:
os.makedirs(os.path.join(basedir,'Subfolder'))
fullpath = os.path.join(basedir,'Subfolder',filename)
... and it works well. Almost. Now I am facing the problem with diacritics in basedir. When basedir contains something like "C:\Úklid\" I get "no such file or directory" error when code is creating Subdirecotry in it. After I rename the source folder to "C:\Uklid\" it works with ease. But I do need it to work with every path valid in Windows OS. Can someone help me with that?
UPDATE 2:
Looks like unicode() did the trick:
sourcename = pdb.gimp_image_get_filename(ima)
basedir = os.path.dirname(sourcename)
if os.path.isdir(os.path.join(basedir,'Subfolder')) is False:
os.makedirs(unicode(os.path.join(basedir,'Subfolder')))
fullpath = os.path.join(basedir,'Subfolder',filename)
Try this out:
import os
sourcename = pdb.gimp_image_get_filename(ima) # gets full name of source file
basedir= os.path.dirname(sourcename) # gets path
name = pdb.gimp_layer_get_name(ima.layers[0]) # gets name of active layer
filename = name+'.png'
fullpath = os.path.join(basedir,'subfolder',filename) # use os.path.join to build paths
# new line vvv
os.makedirs(os.path.join(basedir,'subfolder')) # make directory if it doesn't exist
drawable = pdb.gimp_image_active_drawable(ima)
pdb.file_png_save_defaults(ima, drawable, fullpath, filename) # saves output file

Copying files with specific name to a specific folder using python

I have a following problem (I am on macOS):
12 usb flash drives are mounted in /Volumes and they have names from cam0 to cam11. Each of the drives have a following structure cam0/DCIM/100HDDVR/C00_0001.mp4, cam1/DCIM/100HDDVR/C01_0001.mp4, etc. In each 100HDDVR folder there will be multiple files so for cam0 for example it will be: C00_0001.mp4, C00_0002.mp4, C00_0003.mp4, etc.
Now I would like to copy it to the desktop, lets say, where a new folder will be created called: recording[adds today date] then create a subfolder shoot1 which will be containing files C00_0001.mp4 through C11_0001.mp4 and create subfolder shoot2 that will be containing C00_0002.mp4 through C11_0002.mp4 and so on until all the files from the flash drives are copied.
So far I managed to copy all files from cam0/DCIM/100HDDVR to a new folder recordings/cam0 but it is not automated enough and I am struggling to update it.
def copyy(self):
root.update_idletasks()
self.status['text'] = "Files are being copyied, have patience ;)".format(self.status)
self.source_direcotry0= '/Volumes/CAM0/DCIM/100HDDVR'
self.source_direcotry1= '/Volumes/CAM1/DCIM/100HDDVR'
self.source_direcotry2= '/Volumes/CAM2/DCIM/100HDDVR'
self.source_direcotry3= '/Volumes/CAM3/DCIM/100HDDVR'
self.source_direcotry4= '/Volumes/CAM4/DCIM/100HDDVR'
self.source_direcotry5= '/Volumes/CAM5/DCIM/100HDDVR'
self.source_direcotry6= '/Volumes/CAM6/DCIM/100HDDVR'
self.source_direcotry7= '/Volumes/CAM7/DCIM/100HDDVR'
self.source_direcotry8= '/Volumes/CAM8/DCIM/100HDDVR'
self.source_direcotry9= '/Volumes/CAM9/DCIM/100HDDVR'
self.source_direcotry10= '/Volumes/CAM10/DCIM/100HDDVR'
self.source_direcotry11= '/Volumes/CAM11/DCIM/100HDDVR'
self.path0="recording/CAM0"
self.path1="recording/CAM1"
self.path2="recording/CAM2"
self.path3="recording/CAM3"
self.path4="recording/CAM4"
self.path5="recording/CAM5"
self.path6="recording/CAM6"
self.path7="recording/CAM7"
self.path8="recording/CAM8"
self.path9="recording/CAM9"
self.path10="recording/CAM10"
self.path11="recording/CAM11"
self.cam0=os.path.join(self.Destination.get(), self.path0)
self.cam1=os.path.join(self.Destination.get(), self.path1)
self.cam2=os.path.join(self.Destination.get(), self.path2)
self.cam3=os.path.join(self.Destination.get(), self.path3)
self.cam4=os.path.join(self.Destination.get(), self.path4)
self.cam5=os.path.join(self.Destination.get(), self.path5)
self.cam6=os.path.join(self.Destination.get(), self.path6)
self.cam7=os.path.join(self.Destination.get(), self.path7)
self.cam8=os.path.join(self.Destination.get(), self.path8)
self.cam9=os.path.join(self.Destination.get(), self.path9)
self.cam10=os.path.join(self.Destination.get(), self.path10)
self.cam11=os.path.join(self.Destination.get(), self.path11)
self.p.start(1)
self.work_thread = threading.Thread(target=self.copyy2, args=())
self.work_thread.start()
self.work_thread.join()
self.p.stop()
self.status['text'] = "Files have been copyied".format(self.status)
def copyy2(self):
shutil.copytree(self.source_direcotry0, self.cam0)
shutil.copytree(self.source_direcotry1, self.cam1)
shutil.copytree(self.source_direcotry2, self.cam2)
shutil.copytree(self.source_direcotry3, self.cam3)
shutil.copytree(self.source_direcotry4, self.cam4)
shutil.copytree(self.source_direcotry5, self.cam5)
shutil.copytree(self.source_direcotry6, self.cam6)
shutil.copytree(self.source_direcotry7, self.cam7)
shutil.copytree(self.source_direcotry8, self.cam8)
shutil.copytree(self.source_direcotry9, self.cam9)
shutil.copytree(self.source_direcotry10, self.cam10)
shutil.copytree(self.source_direcotry11, self.cam11)
On top of that how would you tackle this problem so it works on Windows too were when flash drives are mounted you can't see their name and disk letters are always different. Is that even possible?
I hope that I managed to describe it clear enough and thanks in advance for any tips.
I would do it another way around :
get all files path under /Volume
search all files finishing by the extension you Want : [0001.mp4, 0002.mp4, etc..]
copy these files to a subdirectory on your desktop.
For (1) you can use :
def get_filepaths(directory, extension=None):
"""
Generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
import os, ntpath
assert(os.path.isdir(directory)), "The 'directory' parameter is not a directory (%s)"%(directory)
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
if not ntpath.basename(filepath).startswith("."):
file_paths.append(filepath) # Add it to the list.
if extension:return [x for x in file_paths if x.endswith(extension) ]
return file_paths # Self-explanatory
For (2) and (3) you can use the same fonction in a loop :
# this is a pseudo code - no tested and has to be customized
nb_shoot = n
for shoot_n in range(nb_shoot):
# 1) looking for files to copy
nb_zeros = 3 - len(str(shoot_n)) # calculating the number of 0 to put in front of the filename
zeros = "0"*nb_zeros
extension = "%s%shoot_n.mp4"%(zeros, shoot_n)
paths_for_the_shoot = get_filepaths("/Volumes/", extension)
#2) creating a new dir
current_date = time.time()
new_dir_name = "recording_%s/shoot_%s"%(current_date , shoot_n )
os.mkdir(new_dir_name)
for path in paths_for_the_shoot:
#copy file
os.copy(path, new_dir_name)

How can I scrape file names and create directories for each filename in Python?

I'm trying to scrape filenames inside a folder and then make directories for each filename inside another folder. This is what I've got so far but when I run it, it doesn't create the new folders in the destination folder. When I run it in the terminal it doesn't return any errors.
import os
import shutil
folder = "/home/ro/Downloads/uglybettyfanfiction.net/"
destination = "/home/ro/A Python Scripts/dest_test/"
# get each files path name
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
for files in os.listdir(folder):
new_path = folder + files
ensure_dir(new_path)
You've got a few mistakes. No need to use dirname and you should write to your destination, not the same folder:
def ensure_dir(f):
if not os.path.exists(f):
os.mkdir(f)
for files in os.listdir(folder):
new_path = destination + files
ensure_dir(new_path)

Categories

Resources