I got this code, that looks on a directory for .rar files and unrar them to another directory. I want to be able to fetch the name of the file that has been unrared for the purpose to change to the destination directory (which is the file minus the extension) and from there do more work, but I can't do anything else after the if (like, getting the file name with os.path.basename() before executing patoolib.extract_archive(my_file, outdir="/root/tree/def") How can I get the file name and pass that to another function? The code: import shutil, os, patoolib, fnmatch, glob def unrar(): for my_file in glob.glob('/root/tree/down/*'): if fnmatch.fnmatch(my_file, '*.rar'): patoolib.extract_archive( my_file, outdir="/root/tree/def") unrar() Continue reading...