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:
def test_clean_up_logs(path_to_my_log): regex = path_to_my_log + ".*" # i need this because log will append current date time when it's generated print(regex) # i can see it's correct result = subprocess.run(['rm', '-rf', regex]) def test_my_real_test(): # This will run my real test and generate log files
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:
Python3.7 -m pytest /path/to/mydir
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:
$ rm "-rf" "filename.*"
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.
subprocess.run(f'rm -rf {regex}', shell=True)