I am running script to unrar some files and the remove the rar files afterwards. I am doing this by running the command through shell. I have tried several different ways to make the script wait until it's done unpacking the files, but it still goes ahead and deletes the file before they are done being used.
I have tried the code below, which is a no go. I have tried to see if i could get the wait() to work, but also no luck.
Any ideas? running python 2.7
EDIT: I want the script to run the command :)
p = subprocess.Popen('unrar e ' + root + '/' + i + ' ' + testfolder,
bufsize=2048, shell=True,
stdin=subprocess.PIPE)
p.stdin.write('e')
p.communicate()
for root, dirs, files in os.walk(testfolder):
for i in files:
print 'Deleting rar files'
os.remove(i)
for i in os.listdir(testfolder):
if os.path.isdir(testfolder + i):
shutil.rmtree(testfolder + i)
subprocessos.system("some command")is blocking afaik .... so it should block until unrar returns at least (which I assume is when it is finished) ... although i would think subproccess.communicate is also blocking ... my guess is that unrar does not wait until its finished to return an exit codesubprocess.Popenis far safer than usingos.system(), as it's capable of being used without a shell. (Granted, it's not being used that way HERE, but that's bad practice and unsafe).