Skip to content
Advertisement

python3.7 subprocess failed to delete files for me

I have a python script using ‘subprocess’ running linux command to confirm my task is doing the right thing, and it worked well. But i found that at the same time it will generate some log files when running my task. So i added a clean up function to rm log files for me at the beginning. My script is:

JavaScript

but it turns out it did not remove log files for me after i added first test, it still have more and more logs file in my build dir. I run it using:

JavaScript

My question is: 1. Why did not it work? In my second test case, i am using ‘subprocess’ to run a linux command and it worked fine.
2. Is this correct way to clean up log files? i cannot think of a better way to do it automatically. Thanks!

Advertisement

Answer

Why did not it work?

Because the arguments that you gave to your command is passed in quotes and wildcards like * does not work in quotes. Currently the executed command looks like this:

JavaScript

Try this in your terminal and you will see that it will not remove the files that starts with filename..

You need to pass shell = True to execute the command in a shell interpreter and give your command as a single string.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement