I have a bunch of files within a separate drive, a D: drive. My goal was to move all jpg, png, and gif files to a ‘images’ folder and all mp4, mov files to a ‘videos’ folder; except for files within a particular folder.
This is what I used
import os
import shutil
# Locations
mainLoc = r"D:folder"
secLoc = r"D:foldersubfolder" # These are the files to exclude in the shutil.move
imageLoc = r"D:folderImages"
videoLocation = r"D:folderVideos"
# Paths
parentPath = os.path.join(mainLoc)
imagePath = os.path.join(mainLoc, imageLoc)
videoPath = os.path.join(mainLoc, videoLoc)
# Extension types
imageExtensions = {
".jpg",
".png",
".gif"
}
videoExtensions = {
".mov",
".mp4",
}
# Move files
file = []
while file not in os.listdir(secLoc):
for file in os.listdir(parentPath):
filePath = os.path.join(parentPath, file)
if os.path.isfile(filePath):
extension = os.path.splitext(file)[1].lower()
if extension in imageExtensions:
shutil.move(filePath, imagePath)
if extension in videoExtensions:
shutil.move(filePath, videoPath)
I think I should’ve replaced filePath
at the end with file
. When I ran the code, all the files in the loop disappeared, leaving me with only two files Images
and Videos
inside D:folder
. But I had already created an Images file and Videos file before I ran the code, so I’m surprised to see nothing actually went to those folders.
Is there any hope I can recover the files? I understand shutil.move
does not delete files, so there has to be something I can do.