2016-04-26 72 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)