2016-04-26 37 views
0

我需要將幾個目錄中的圖像移動到一個,並在移動之前和之後捕獲這些文件的元數據。創建後使用python class.instance

在每個目錄:

  • 閱讀來自indexfile.csv JPG圖像,包括每個圖像

  • 上傳對應的圖像文件,以谷歌驅動器上的元數據的索引,與元數據

  • 添加條目到uberindex.csv,其中包括indexfile.csv的元數據和上傳後的谷歌驅動器的文件url

我的計劃是爲indexfile.csv的每一行創建一個類ybpic() - def下面的類的實例,並使用該實例來標識要移動的實際文件(它是索引文件中的參考),保存來自indexfile.csv的元數據,然後在最終將所有實例寫出到uberindex.csv之前使用谷歌驅動器上載(其他元數據)的結果更新ybpic.instance。

我知道當答案出現時我會踢自己(真正的noob)。

我可以csv.reader indexfile.csv到一個ybpic.instance,但我不能引用每個實例分別以後使用或更新實例。 我可以將indexfile.csv中的行附加到indexlist [],並且我可以將更新後的列表返回給調用者,但我不知道更新該列表行的好方法,對於相應的圖像文件,稍後使用新的元數據。

這裏的ybpic高清

class ybpic(): 

    def __init__(self,FileID, PHOTO, Source, Vintage, Students,Folder,Log): 
     self.GOBJ=" " 
     self.PicID=" " 
     self.FileID=FileID 
     self.PHOTO=PHOTO 
     self.Source=Source 
     self.Students=Students 
     self.Vintage=Vintage 
     self.MultipleStudents=" " 
     self.CurrentTeacher=" " 
     self.Folder=Folder ## This may be either the local folder or the drive folder attr 
     self.Room=" " 
     self.Log=Log ## The source csvfile from which the FileID came 

這裏是填充實例和列表功能。 indexfile.csv作爲photolog傳遞,而cwd只是工作目錄:

def ReadIndex(photolog, cwd, indexlist) : 
    """ Read the CSV log file into an instance of YBPic. """ 

    with open(photolog,'r') as indexin : 
     readout = csv.reader(indexin) 

    for row in readout: 
     indexrow=ybpic(row[0],row[1],row[2],row[3],row[4],cwd,photolog) 

     indexlist.append(row)  ### THIS WORKS TO APPEND TO THE LIST 
            ### THAT WAS PASSED TO ReadIndex 

return(indexlist) 

任何和所有的幫助,非常感謝。

+0

不直接相關,但請查看'glob',特別是'glob.glob'(用於使用通配符獲取文件列表)和'shutil'用於複製文件。兩者都是內置庫的一部分。 – Benjamin

回答

0

除了使用列表,您可以使用帶PhotoID的對象字典作爲關鍵字(假設它存儲在行[0]中)。

def ReadIndex(photolog, cwd, indexlist) : 
    """ Read the CSV log file into an instance of YBPic. """ 

    ybpic_dict = {}  

    with open(photolog,'r') as indexin : 
     readout = csv.reader(indexin) 

    for row in readout: 
     ybpic_dict[row[0]] = ybpic(row[0],row[1],row[2],row[3],row[4],cwd,photolog) 

    return ybpic_dict 

然後,當你需要更新的屬性後

ybpic_dict[PhotoID].update(...) 
0

好了,因爲我發現自己的答案,不踢是爲了....

商店ybpic.instance對象在列表中。

答案是,在for循環從INDEXFILE的行創建ybpic的實例,而不是將相關的實例在列表中被傳遞迴調用方,追加將該實例的實際對象插入到列表中,然後傳回給調用者。一旦我回到調用函數中,我就可以訪問對象(實例)。

我不確定這是否是最好的答案,但它是讓我轉向下一個的答案。

新代碼:

高清ReadIndex(photolog,CWD,indexlist): 「」 「閱讀CSV日誌文件到YBPic的一個實例。 」「」

with open(photolog,'r') as indexin : 
     readout = csv.reader(indexin) 

    for row in readout: 
     indexrow=ybpic(row[0],row[1],row[2],row[3],row[4],cwd,photolog) 

     indexlist.append(indexrow)  ## Store the ybpic.indexrow  instance 

return(indexlist)