说明:
1. open("index.html","w",encoding="utf-8"),open的第三个参数可以设置编码格式。
2. 代码作用是给每个文件夹新建一个index.html文件,显示文件夹里的所有图片。
1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 import os 4 5 pathMain = r"E:manhuamh" 6 # print(path) 7 for paths, folders, filenames in os.walk(pathMain): 8 # print(folders) 9 for folder in folders: 10 root = os.path.join(paths,folder) 11 page = len(os.listdir(root)) 12 # print(page) 13 os.chdir(root) 14 html = open("index.html","w",encoding="utf-8") 15 text = ''' 16 <!DOCTYPE html> 17 <html lang="en"> 18 <head> 19 <meta charset="UTF-8"> 20 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" /> 21 <title>'''+folder+'''</title> 22 <style type="text/css"> 23 body{ 24 text-align: center; 25 } 26 </style> 27 </head> 28 <body> 29 <h1>'''+folder+'''</h1> 30 <div id="div"> 31 </div> 32 </body> 33 <script type="text/javascript"> 34 var page = '''+str(page)+'''; 35 var text = ""; 36 for (var i = 0; i < page; i++) { 37 text += '<img src="'+i+'.jpg" alt="">'; 38 } 39 var div = document.getElementById("div"); 40 div.innerHTML = text; 41 </script> 42 </html> 43 ''' 44 html.write(text) 45 html.close()