设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 10319|回复: 1
打印 上一主题 下一主题

被提示can only concatenate list (not "str") to list 求解答

[复制链接]

7

主题

0

好友

49

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2017-10-6 14:37:37 |只看该作者 |倒序浏览
本帖最后由 incipient 于 2017-10-6 14:40 编辑

代码如下:
import os
import time
source = ["E:\\Python learning\__pycache__"]
target_dir = ["F:\\Beckup"]
target = target_dir + os.sep + time.strftime("%Y%m%d%H%M%S") + ".zip"
if not os.path.exists(target_dir):
    os.mkdir(target_dir)

zip_command = 'zip -r {0} {1}'.format(target,
                                      ' '.join(source))
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
    print('Successful backup to',target)
else:
    print('Backup FAILED')

运行之后被提示:
target = target_dir + os.sep + time.strftime(("%Y%m%d%H%M%S") + ".zip")
TypeError: can only concatenate list (not "str") to list

目的:
1、将文件自动压缩到另一个文件中备份
2、以zip文件形式存放
3、备份文件以当前时间命名
4、验证是否备份成功
系统:win10.0.1_64位,IDLE:Python 3.7.0a1


回复

使用道具 举报

7

主题

0

好友

49

积分

新手上路

Rank: 1

沙发
发表于 2017-10-6 15:55:12 |只看该作者
已经解决了:问题不在被提示出现错误的行,而在第3、4行
source = ["E:\\Python learning\__pycache__"]
target_dir = ["F:\\Beckup"]
修改为:
source = [‘"E:\\Python learning\__pycache__"’]
target_dir = ‘F:\\Beckup’
修改过程:增加单引号、将双引号换成单引号就行了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-11 15:53 , Processed in 0.025810 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部