problem with moving files with extensions with shutil - python

I have the following code that should move files from one directory to another, the problem is when I run the code it just creates the folder and not moving any file to it. can anyone help me with that?
import os
import glob
import shutil
def remove_ext(list_of_pathnames):
return [os.path.splitext(filename)[0] for filename in list_of_pathnames]
path = os.getcwd()
os.chdir("D:\\TomProject\\Images\\")
os.mkdir("image_with_xml") # create a new folder
newpath = os.path.join("D:\\TomProject\\Images\\" ,"image_with_xml") #
made it os
independent...
list_of_jpegs = glob.glob(path+"\\*.jpeg")
list_of_xmls = glob.glob(path+"\\*.xml")
jpegs_without_extension = remove_ext(list_of_jpegs)
xmls_without_extension = remove_ext(list_of_xmls)
for filename in jpegs_without_extension:
if filename in xmls_without_extension:
shutil.move(filename + '.jpg', newpath) # move image to new path.
shutil.move(filename + '.xml', newpath)

You first use ".jpeg" as extension but then when you move mistakenly use ".jpg".

Related

how to copy selected file without moving it to another directory in python

i am trying to copy the selected images to another directory. I have tried shutil.move() and shutil.copy and shutil.copy2. these move the selected file. but i want to just make copy that and move that copy to required dir.
code
cwd = os.getcwd()
filepath = os.path.dirname(os.path.abspath(filepath))
shutil.move('{0}/{1}'.format(filepath, filename), '{0}/modeldata/temp/{1}'.format(cwd, filename))
You can use shutil.copyfile(src, dst)
import os
import shutil
cwd = os.getcwd()
filepath = os.path.dirname(os.path.abspath(filepath))
shutil.copyfile(os.path.join(filepath, filename), os.path.join(cwd, 'modeldata/temp/' filename))
Read more here:
https://docs.python.org/3/library/shutil.html

Copy files with specific text at the end of their (file) names from one folder to other folder

I have 23000 files in one folder and want to copy files with their names ending with some specific text to other folder using python. I used the following code, though it runs successfully but it doesn't copy the file in the destination folder.
import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"
for f in os.listdir(path):
if f.endswith("clip_2"):
shutil.copyfile(os.path.join(path, f), outpath)
you are trying to copy files to a single filename again and again, so to fix it you will have to add the filename with outpath for every file
import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"
if not os.path.isdir(outpath):
os.mkdir(outpath)
else:
shutil.rmtree(outpath)
os.mkdir(outpath)
for f in os.listdir(path):
f2, ext = os.path.splitext(f)
if f2.endswith("clip_2"):
shutil.copyfile(os.path.join(path, f), os.path.join(outpath, f))
before running the above code make sure that you remove D:/Snow_new/testout completly

Copy files along with file paths from a list to a folder or another path

I have a list containing files along with their paths. I need to copy or move the files with their entire path into another directory or folder.
I have tried as follows but path unable to copy the path and only files are being copied.
import shutil
list_l1 = ['/home/Test//A/Aa/hello1.c', '/home/Test/C/Aa/hello1.c', '/home/Test/B/Aa/hello1.c']
for source in list_l1:
shutil.move(source, '/home/Test/sample_try/sample/')
You probably want to use os.makedirs() to make the nested directories. You might want to separate the paths in your list_l1 into directory and filename parts first, and use os.path.exists() to check if the directory exists before attempting to create it.
You could try:
import shutil
import os
list_l1 = ['/home/Test//A/Aa/hello1.c', '/home/Test/C/Aa/hello1.c', '/home/Test/B/Aa/hello1.c']
dest = '/home/Test/sample_try/sample'
for source in list_l1:
dirname, filename = os.path.split(source)
if not os.path.exists(f'{dest}/{dirname}'):
os.makedirs(f'{dest}/{dirname}')
shutil.copy(source, f'{dest}/{source}')
You can try making the directories first like below or some other library.
import shutil
from pathlib import Path
list_l1 = ['./A/Aa/hello1.c', './B/Aa/hello1.c']
new_parent = './C'
for source in list_l1:
path_list = source.split('/')
file = path_list.pop()
new_path = path_list.pop(0)
dirs = '/'.join(path_list)
p = new_parent + '/' + dirs + '/'
path = Path(p)
path.mkdir(parents=True, exist_ok=True)
shutil.move(source, p)

Recursively rename file extensions

I am having a difficult time creating a python script that will rename file extensions in a folder and continue to do so in sub directories. Here is the script I have thus far; it can only rename files in the top directory:
#!/usr/bin/python
# Usage: python rename_file_extensions.py
import os
import sys
for filename in os.listdir ("C:\\Users\\username\\Desktop\\test\\"): # parse through file list in the folder "test"
if filename.find(".jpg") > 0: # if an .jpg is found
newfilename = filename.replace(".jpg","jpeg") # convert .jpg to jpeg
os.rename(filename, newfilename) # rename the file
import os
import sys
directory = os.path.dirname(os.path.realpath(sys.argv[0])) #get the directory of your script
for subdir, dirs, files in os.walk(directory):
for filename in files:
if filename.find('.jpg') > 0:
subdirectoryPath = os.path.relpath(subdir, directory) #get the path to your subdirectory
filePath = os.path.join(subdirectoryPath, filename) #get the path to your file
newFilePath = filePath.replace(".jpg",".jpeg") #create the new name
os.rename(filePath, newFilePath) #rename your file
I modified Jaron's answer with the path to the file and the complete example of renaming the file
I modified the answer of Hector Rodriguez Jr. a little bit because it would replace ANY occurance of ".jpg" in the path, e.g. /path/to/my.jpg.files/001.jpg would become /path/to/my.jpeg.files/001.jpeg, which is not what you wanted, right?
Although it is generally not a good idea to use dots "." in a folder name, it can happen...
import os
import sys
directory = os.path.dirname(os.path.realpath(sys.argv[0])) # directory of your script
for subdir, dirs, files in os.walk(directory):
for filename in files:
if filename.find('.jpg') > 0:
newFilename = filename.replace(".jpg", ".jpeg") # replace only in filename
subdirectoryPath = os.path.relpath(subdir, directory) # path to subdirectory
filePath = os.path.join(subdirectoryPath, filename) # path to file
newFilePath = os.path.join(subdirectoryPath, newFilename) # new path
os.rename(filePath, newFilePath) # rename
You can process the directory like this:
import os
def process_directory(root):
for item in os.listdir(root):
if os.path.isdir(item):
print("is directory", item)
process_directory(item)
else:
print(item)
#Do stuff
process_directory(os.getcwd())
Although, this isn't really necessary. Simply use os.walk which will iterate through all toplevel and further directories / files
Do it like this:
for subdir, dirs, files in os.walk(root):
for f in files:
if f.find('.jpg') > 0:
#The rest of your stuff
That should do exactly what you want.

error moving file with shutil

I am trying to create a simple function that finds files that begin with a certain string and then move them to a new directory but I keep getting the following kinds of errors from shutil "IOError: [Errno 2] No such file or directory: '18-1.pdf'", even though the file exists.
import os
import shutil
def mv_files(current_dir,start):
# start of file name
start = str(start)
# new directory ro move files in to
new_dir = current_dir + "/chap_"+ start
for _file in os.listdir(current_dir):
# if directory does not exist, create it
if not os.path.exists(new_dir):
os.mkdir(new_dir)
# find files beginning with start and move them to new dir
if _file.startswith(start):
shutil.move(_file, new_dir)
Am I using shutil incorrectly?
correct code:
import os
import shutil
def mv_files(current_dir,start):
# start of file name
start = str(start)
# new directory ro move files in to
new_dir = current_dir + "/chap_" + start
for _file in os.listdir(current_dir):
# if directory does not exist, create it
if not os.path.exists(new_dir):
os.mkdir(new_dir)
# find files beginning with start and move them to new dir
if _file.startswith(start):
shutil.move(current_dir+"/"+_file, new_dir)
It looks like you aren't giving the full path to shutil.move. Try:
if _file.startswith(start):
shutil.move(os.path.abspath(_file), new_dir)
If that fails, try printing _file, and new_dir along with the result of os.getcwd() and adding them to your answer.

Categories