Home > Back-end >  how to run 2 functions in python
how to run 2 functions in python

Time:11-21

hi im pretty new to python and i have a problem that i dont know how to run this 2 functions. can someone give me directions how i can do it?

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)
       

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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()
  • Related