Python 子进程脚本失败

Python subprocess script failing

已编写以下脚本来删除文件夹中与 "keep" 期间日期不匹配的文件。例如。删除除部分匹配此名称的文件外的所有文件。

该命令在 shell 中有效,但在调用子进程时失败。

/bin/rm /home/backups/!(*"20170920"*|*"20170919"*|*"20170918"*|*"20170917"*|*"20170916"*|*"20170915"*|*"20170914"*)
#!/usr/bin/env python
from datetime import datetime
from datetime import timedelta
import subprocess

### Editable Variables
keepdays=7
location="/home/backups"

count=0
date_string=''
for count in range(0,keepdays):
    if(date_string!=""):
        date_string+="|"
    keepdate = (datetime.now() - timedelta(days=count)).strftime("%Y%m%d")
    date_string+="*\""+keepdate+"\"*"

full_cmd="/bin/rm "+location+"/!("+date_string+")"
subprocess.call([full_cmd], shell=True)

脚本是这样的returns:

#./test.py

/bin/rm /home/backups/!(*"20170920"*|*"20170919"*|*"20170918"*|*"20170917"*|*"20170916"*|*"20170915"*|*"20170914"*)
/bin/sh: 1: Syntax error: "(" unexpected

Python 版本是 Python 2.7.12

正如@hjpotter 所说,子进程将使用 /bin/sh 作为默认 shell,它不支持您想要的那种 globbing。参见 official documentation。您可以使用 executable 参数将其更改为 subprocess.call(),并使用更合适的 shell(例如 /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)