2012-07-24 41 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功能。

相關問題