Python:“只能将列表(而不是“ str”)连接到列表”

时间:2019-08-18 13:10:16

标签: python string list

我正在使用Python制作备份程序。它给了我这个错误“总是只能连接列表(而不是“ str”)来列出”。

我试图进行一些更改,但是没有用。试图更改备份文件的路径。

import os
import time

source = ['C:\\OSPanel']

targetdir = ['C:\\Backup']

target = targetdir + os.sep + time.strftime() + '.zip'

zipcommand = "zip -qr {0} {1}".format(target, ' '.join(source))

if os.system(zipcommand) == 0:
    print ("Backup copy has been saved in", target)
else:
    print ("Can't backup")

我希望输出-“备份副本已保存在...中” 但是我得到这个,错误。

2 个答案:

答案 0 :(得分:1)

source = ["C:\\src"]
target = ["C:\\treg"]
import os, time, datatetime
now = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M_%S")
td = [ os.path.join(t, now + '.zip') for t in target]
>>> td
['C:\\treg\\08_18_2019_15_20_28.zip']

...

答案 1 :(得分:0)

运行此:

import os

from datetime import datetime
source = 'C:\\OSPanel'

targetdir = 'C:\\Backup'

now = datetime.now()

target = targetdir + os.sep + now.strftime("%m/%d/%Y, %H:%M:%S") + '.zip'

zipcommand = "zip -qr {0} {1}".format(target, ' '.join(source))

if os.system(zipcommand) == 0:
    print ("Backup copy has been saved in", target)
else:
    print ("Can't backup")