TypeError:只能将元组(不是“str”)连接到元组第6行

时间:2016-09-26 09:09:25

标签: python

import os

import time

source = [r'C:\\Documents','/home/swaroop/byte', '/home/swaroop/bin']

target_dir =r'C:\\Documents','/mnt/e/backup/' 

today = target_dir + time.strftime("%Y%m%d")

now = time.strftime('%H%M%S')

os.path.exists(today)

os.mkdir(today)

print 'Successful created directory', today

target = today + os.sep + now + '.zip'

zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

if os.system(zip_command) == 0:

print 'Successful backup to', target

else:
    print 'Backup FAILED'

today = target_dir + time.strftime("%Y%m%d")

请求帮助

TypeError:只能将元组(不是“str”)连接到元组

1 个答案:

答案 0 :(得分:0)

检查逗号为@MosesKoledoye说,book有:

# 1. The files and directories to be backed up are
# specified in a list.
# Example on Windows:
# source = ['"C:\\My Documents"', 'C:\\Code']     # Example on Mac OS X and Linux:
source = ['/Users/swa/notes']
# Notice we had to use double quotes inside the string # for names with spaces in it.

# 2. The backup must be stored in a
# main backup directory
# Example on Windows:
# target_dir = 'E:\\Backup'
# Example on Mac OS X and Linux:
target_dir = '/Users/swa/backup'
# Remember to change this to which folder you will be using

您的代码有:

source = [r'C:\\Documents','/home/swaroop/byte', '/home/swaroop/bin']

target_dir =r'C:\\Documents','/mnt/e/backup/' 

target_dir分配中,逗号使分配的右侧为元组。要将两个字符串连接在一起,请使用+,而不是逗号:

 target_dir =r'C:\\Documents' + '/mnt/e/backup/' 

更好的是,使用单个字符串。 但是/mnt是Linux目录名,而不是Windows目录名。我怀疑你确实需要:

 target_dir = '/mnt/e/backup/' 

您还将Windows路径设为原始字符串,这意味着将保留两个反斜杠。要么这样做:

'C:\\Documents'

或者这个:

r'C:\Documents'

(除非你确实想要\\

编辑:我刚注意到你也有缩进问题:

if os.system(zip_command) == 0:
print 'Successful backup to', target
else:

应该是:

if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:

最后,当你说"我复制所有的代码"它失败了,看看你的地方与书中所说的不同。