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

Python TypeError:只能将元组(而不是“str”)连接到元组第6行,python,Python,请帮忙 TypeError:只能将元组(而不是“str”)连接到元组检查逗号,正如@MosesKoledoye所说的,它有: 您的代码有: # 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: s

请帮忙


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

检查逗号,正如@MosesKoledoye所说的,它有:

您的代码有:

# 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
target\u dir
赋值中,逗号使赋值的右侧成为元组。要将两个字符串连接在一起,请使用
+
,而不是逗号:

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

target_dir =r'C:\\Documents','/mnt/e/backup/' 
更好的方法是使用单个字符串但是,
/mnt
是Linux目录名,而不是Windows目录名。我想你确实需要:

 target_dir =r'C:\\Documents' + '/mnt/e/backup/' 
您还将Windows路径设置为原始字符串,这意味着将保留两个反斜杠。要么这样做:

 target_dir = '/mnt/e/backup/' 
或者这个:

'C:\\Documents'
(除非或当然您确实想要
\\

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

r'C:\Documents'
应该是:

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

最后,当你说“我复制所有代码”但失败时,看看你的代码与书中所说的有什么不同。

target\u dir=r'C:\\Documents',“/mnt/e/backup/”
中有一个逗号分隔这些字符串,这使它成为一个元组。如果您试图在
C:\Documents
/mnt/e/backup
中同时创建一个目录,我正在从《python的一个字节》一书中学习python。我来到页面编写了一个python脚本示例备份脚本-第一个版本,我复制了他编写的python shell的所有代码,但仍然出错
if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else: