Get all the pictures in the PIC subdirectory in this directory (Jpg,png,bmp,gif, etc.), and then generate a image.html file that opens the HTML file to view all the pictures in the PIC subdirectory in the browser.
#Coding:utf-8ImportOs,sysdefshowimageinhtml (Imagetypes,savedir): Files=getallfiles (savedir+'\\pic') Images=[f forFinchFilesifF[f.rfind ('.') + 1:]inchImagetypes] Images=[item forIteminchImagesifOs.path.getsize (item) >5*1024] Images=['pic'+item[item.rfind ('\\'):] forIteminchImages] NewFile='%s\\%s'% (Savedir,'images.html') with open (NewFile,'W') as F:f.write ('<div>') forImageinchImages:f.write ("\n"%image) F.write ('</div>') Print 'Success,images is wrapped up in%s'%NewFiledefgetallfiles (directory): Files=[] forDirpath, Dirnames,filenamesinchos.walk (directory):iffilenames!=[]: forFileinchFilenames:files.append (Dirpath+'\\'+file) Files.sort (key=len)returnFiles#gets the current path of the script filedefCur_file_dir ():#Get Script PathPath =Sys.path[0]#determine whether the script file or Py2exe compiled file, if it is a script file, the script is returned to the directory, if it is Py2exe compiled file, then the compiled file path is returned ifOs.path.isdir (path):returnPathelifos.path.isfile (path):returnos.path.dirname (path)if __name__=='__main__': Savedir=Cur_file_dir ()#获取当前 The file path of the. py script file Showimageinhtml (('jpg' , ' png ', ' gif '), Savedir)#浏览所有jpg, png,gif file
"Python" automatically generates HTML files to view all pictures in the specified directory