"Python" automatically generates HTML files to view all pictures in the specified directory

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.