Skip to content

Instantly share code, notes, and snippets.

@ola0x
Created March 24, 2022 23:27
Show Gist options
  • Save ola0x/35e3bff726440cd91cae218af5d0f60a to your computer and use it in GitHub Desktop.
Save ola0x/35e3bff726440cd91cae218af5d0f60a to your computer and use it in GitHub Desktop.
copy xml file and image jpg with the same name
import os
import shutil
dir_path = "data"
files = os.listdir(dir_path)
path = os.getcwd()
olddir_path = os.path.join(path, dir_path)
newpath = os.path.join(path,"newdata")
print(newpath)
print(olddir_path)
folders = []
def get_xml_jpg_name_pairs():
for f in os.listdir(dir_path): #List the all the files in the test_dir folder
if os.path.splitext(f)[1].lower() in (".xml"): # check for files with (.jpg, .jpeg, .jfif) extension
folders.append(os.path.splitext(f)[0].lower())
return folders
folders = get_xml_jpg_name_pairs()
print(folders)
for files in folders:
print("moving", files) #remove
shutil.move((os.path.join(olddir_path, files + '.jpg')), newpath) # move image to new path.
shutil.move((os.path.join(olddir_path, files + '.xml')), newpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment