/bin/sh
作为默认的shell,它不支持您想要做的那种globbing。看见
official documentation
. 您可以使用
executable
参数到
subprocess.call()
/bin/bash
或
/bin/zsh
subprocess.call([full_cmd], executable="/bin/bash", shell=True)
但是Python本身可以更好地为您提供服务,您不需要调用子进程来删除文件:
#!/usr/bin/env python
from datetime import datetime
from datetime import timedelta
import re
import os
import os.path
### Editable Variables
keepdays=7
location="/home/backups"
now = datetime.now()
keeppatterns = set((now - timedelta(days=count)).strftime("%Y%m%d") for count in range(0, keepdays))
for filename in os.listdir(location):
dates = set(re.findall(r"\d{8}", filename))
if not dates or dates.isdisjoint(keeppatterns):
abs_path = os.path.join(location, filename)
print("I am about to remove", abs_path)
# uncomment the line below when you are sure it won't delete any valuable file
#os.path.delete(abs_path)