在python中使用subprocess.call压缩文件

在python中使用subprocess.call压缩文件,python,zip,Python,Zip,我的服务器有点问题 故事是这样的: 我有一个NAS,可以处理我所在域的电子邮件。它工作得很好,但是它没有为电子邮件提供任何备份软件,但是它包含的邮箱有点奇怪 它将它们包含在用户的主目录中,如“/home/user123/.Mailbox” 所以,如果我想收集这些邮箱并存档它们,我必须编写一点代码 问题是,虽然它上面有一种Linux,但它没有bash,而sh没有for循环 另一方面,它有python模块,我开始在其中编写一些脚本,但后来我坚持 当我想使用ZIP打包和压缩文件时,我注意到如果我通过变

我的服务器有点问题

故事是这样的: 我有一个NAS,可以处理我所在域的电子邮件。它工作得很好,但是它没有为电子邮件提供任何备份软件,但是它包含的邮箱有点奇怪

它将它们包含在用户的主目录中,如“/home/user123/.Mailbox”

所以,如果我想收集这些邮箱并存档它们,我必须编写一点代码

问题是,虽然它上面有一种Linux,但它没有bash,而sh没有for循环

另一方面,它有python模块,我开始在其中编写一些脚本,但后来我坚持

当我想使用ZIP打包和压缩文件时,我注意到如果我通过变量传递ZIP命令的参数,它无法处理这些参数之间的空格

有谁能给我一个解决办法吗

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from os import walk,path
import subprocess
import datetime

sourcepath="/home/"
savepath="/backups"
foldername=[]
tarparams=''
dt=str(datetime.date.today())

## get to know the directories in /home    
for (dirpath,dirnames,filenames) in walk(sourcepath):
    foldername.extend(dirnames)
    break
##

## check if the save path exists and if it does create the parameters of the compression
if not path.exists(savepath):
    print "Save path does not exist"
    exit()
else:
    for userhomedir in foldername:
        if path.exists(sourcepath+userhomedir+"/.Maildir"):
#           tarparams=tarparams+'"'+sourcepath+userhomedir+"/.Maildir"+'",'
            tarparams=tarparams+sourcepath+userhomedir+"/.Maildir "
        else:
        None
##

## call the zip command and compress the mailboxes with the highest compression
#subprocess.call(["zip","-r","-9",savepath+"/mailbkp-"+dt,tarparams])
subprocess.call(["zip","-r","-9",savepath+"/mailbkp-"+dt]+tarparams.split())
##

提前感谢您。:-)

[“zip”、“-r”、“-9”、savepath+”/mailbkp-“+dt]+tarparams.split()
?您可能希望查看,这将使任务更容易执行您确定posix shell没有for循环吗?如果没有,您可以使用while with
|
read
。看起来您每次备份都会重新保存所有内容(希望保存到不同的物理驱动器)。在这种情况下,考虑RSYNC代替。非常感谢你们大家!:-)是的,它将每周保存到外部硬盘。