2012-07-24 63 views
0

您好,我正在尝试构建一个工具来压缩文件夹列表并重命名压缩文件,这个我想压缩的文件夹名称列表位于a中。 txt文件,该.TXT是这样的:在列表中的每个项目上使用函数python

james, 5005 
kyle, 02939 
Betty, 40234 

我已经使用了多种方法,试图建立这个代码,但我不断收到一个python错误设置的对象不是标化的,我不知道该怎么做纠正这一点,并从这里继续下去。我可以不使用shutil.make_archive与字典或可以使用列表吗?因为我想在第一列下运行该函数,并使用第二列重命名我创建的文件。我正在使用python 3,并且任何帮助都会很棒!

import os 
import shutil 
x = input("Input Path your user list: ") 
filename = input("Input user file name: ") 
changedir = input("Input where your folders are: ") 
os.chdir(changedir) 

userfile = x + filename + ".txt" 
print("Awesome your path is now", userfile) 
with open(userfile, "rt") as userfileone: 
    count = 0 
    while 1: 
     buffer = userfileone.read(8192*1024) 
     if not buffer: break 
     count += buffer.count('\n') 
     print("It is indicated that there are", count + 1, "in the file") 
with open(userfile, "rt") as f: 
     lines = f.readlines() 
dic = {} 
for x in lines: 
    x = x.strip().split(',') 
    dic[x[0]]=tuple(x[1:]) 

for i in dic: 
    shutil.make_archive(i, "zip", dic[i]) 
+0

目前还不清楚这段代码的作用:为什么要打开文件两次?为什么它会计数'\ n'个字符,而不是使用结果?为什么它读取元组中的第二列?当你说你想要第二列作为名字时,为什么用第一列的名字创建的档案? – 2013-01-17 22:17:45

回答

1

好像你正在寻找map功能。

相关问题