When I first coded this in to a single function, it worked. But I want to do more things when I am checking for files in to the directory, so I have divided the code in two functions : One checks for files on a directory that end with *.rar extension, if it finds a file that matches, it uncompresses it to a directory.
import shutil, os, patoolib, fnmatch, glob
def unrar():
patoolib.extract_archive(file, outdir="/root/tree/def")
def chktree():
for file in glob.glob('/root/tree/down/*'):
if fnmatch.fnmatch(file, '*.rar'):
unrar()
chktree()
Executing unrar()
after the if
from the function chktree():
does not work. I would like to know what I am doing wrong, this is the output :
Traceback (most recent call last):
File "autotube.py", line 16, in <module>
chktree()
File "autotube.py", line 14, in chktree
unrar()
File "autotube.py", line 6, in unrar
patoolib.extract_archive(file, outdir="/root/tree/def")
File "/usr/local/lib/python2.7/dist-packages/patoolib/__init__.py", line 676, in extract_archive
util.check_existing_filename(archive)
File "/usr/local/lib/python2.7/dist-packages/patoolib/util.py", line 389, in check_existing_filename
if not os.path.exists(filename):
File "/usr/lib/python2.7/genericpath.py", line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, type found