I'm writing a script that will move files into a .trash directory in a user's home folder. I want to add the ability to empty the trash directory by calling rm -rf /home/user/.trash/*
using python's subprocess.call()
~$ touch file1
~$ trash file1
['mv', 'file1', '/home/rodney/.trash/']
~$ ls .trash
file1
~$ trash --empty
['rm', '-rf', '/home/rodney/.trash/*']
~$ ls .trash
file1
As you can see the rm command did not remove the contents of the trash. However if I execute the command directly on the command line it works.
~$ rm -rf /home/rodney/.trash/*
~$ ls .trash
~$
The output is from the following code
print(cmd)
subprocess.call(cmd)
What is weird about this is if I exclude the * from the last argument in the cmd list then the subprocess call works but also removes the entire .trash directory. I do not want to delete the .trash directory; only everything under it.
To sum up the question
This works
import subprocess
subprocess.call(['rm', '-rf', '/home/rodney/.trash/'])
This does not
import subprocess
subprocess.call(['rm', '-rf', '/home/rodney/.trash/*'])
Why?
rm
to do something as simple as deleting files in python? – Marquesan*
does not match files that begin with a dot. – Marquesan