how to move files with the exact same name of the xml file to another directory in pyhon

Question:

hi I have the following code that works just fine but I do not know how to move the matching name files to the same directory. for example I have 3 files with the same name (xml, jpeg, txt) when I move the xml file I want all the files with the same name to move with it. I was looking in the forum and did not find anything.

import shutil
from pathlib import Path
from xml.etree import ElementTree as ET


def contains_drone(path):
 tree = ET.parse(path.as_posix())
 root = tree.getroot()
 for obj in root.findall('object'):
    rank = obj.find('name').text
    if rank == 'car':           
         return True
 return False


def move_drone_files(src="D:\TomProject\Images\", 
dst="D:\TomProject\Done"):
 src, dst = Path(src), Path(dst)
 for path in src.iterdir():
    if path.suffix == '.xml' and contains_drone(path):
        print(f'Moving {path.as_posix()} to {dst.as_posix()}')
        shutil.move(path, dst)

if __name__ == "__main__":
    move_drone_files()
       
Asked By: tom meskin

||

Answers:

You should do something like this:

import shutil
from pathlib import Path
from xml.etree import ElementTree as ET


 def contains_drone(path):
    tree = ET.parse(path.as_posix())
    root = tree.getroot()
    for obj in root.findall('object'):
        rank = obj.find('name').text
        if rank == 'drone':           
             return True
    return False
    

def move_drone_files(src='D:\TomProject\Images', dst='D:\TomProject\Images\Done'):
    src, dst = Path(src), Path(dst)
    for path in src.iterdir():
       if path.suffix == '.xml' and contains_drone(path):
           print(f'Moving {path.as_posix()} to {dst.as_posix()}')
           shutil.move(path, dst)

if __name__=='__main__':
    move_drone_files()

Then simply execute your file with python3 file.py and the code in main will be executed.

Answered By: Kins

You need to add if __name__ == "__main__": at the end of the file, with the function you want to call:

import shutil
from pathlib import Path
from xml.etree import ElementTree as ET


def contains_drone(path):
    tree = ET.parse(path.as_posix())
    root = tree.getroot()
    for obj in root.findall('object'):
        rank = obj.find('name').text
        if rank == 'drone':           
             return True
    return False
    

def move_drone_files(src='D:\TomProject\Images', dst='D:\TomProject\Images\Done'):
    src, dst = Path(src), Path(dst)
    for path in src.iterdir():
       if path.suffix == '.xml' and contains_drone(path):
           print(f'Moving {path.as_posix()} to {dst.as_posix()}')
           shutil.move(path, dst)

if __name__ == "__main__":
    move_drone_files()

In your code, move_drone_files() is calling contains_drone(path) inside itself (see line if path.suffix == '.xml' and contains_drone(path):), so it seems that you only need to call move_drone_files() in the main section. Then you just need to execute the python script with a cmd like: python script.py, python3 script.py or python3.X script.py depending on which python version you installed.

PD: I fixed some typos and tab errors you had in the code you posted

Answered By: victorperezpiqueras

I guess there is a syntax error in your code: replace src='D:\TomProject\Images, dst='D:\TomProject\Images\Done' with src="D:\TomProject\Images", dst="D:\TomProject\Images\Done".

Furthermore, remove the four spaces in front of def move_drone_files(...), otherwise python will throw a syntax error.

To call one of these functions, you must type:

containsdrone('path/to/file')
# and
move_drone_files()

The modified code should look like this:

import shutil
from pathlib import Path
from xml.etree import ElementTree as ET


def contains_drone(path):
    tree = ET.parse(path.as_posix())
    root = tree.getroot()
    for obj in root.findall('object'):
        rank = obj.find('name').text
        if rank == 'drone':           
             return True
    return False
    

def move_drone_files(src="D:\TomProject\Images", dst="D:\TomProject\Images\Done"):
    src, dst = Path(src), Path(dst)
    for path in src.iterdir():
       if path.suffix == '.xml' and contains_drone(path):
           print(f'Moving {path.as_posix()} to {dst.as_posix()}')
           shutil.move(path, dst)


containsdrone('path/to/file')
move_drone_files()
Answered By: Dominik Lovetinsky
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.