一般Python语法错误与三引号

时间:2015-01-25 01:17:45

标签: python-2.7

我正在编写一个Python脚本,它将图像目录中的每个文件都列为html文件。在下面的脚本中,我得到了一个"无效的语法"错误,它指向第33行,但我不知道是什么导致它具体。

以下是代码:

import zipfile
import paginate
from os import listdir
from os.path import isfile, join


# def unzip(source_filename, dest_dir):
#     with zipfile.ZipFile(source_filename) as zf:
#         zf.extractall(dest_dir)

# unzip('test.zip', '')

onlyfiles = [ f for f in listdir('images') if isfile(join('images',f)) ]

page = 1
my_page = paginate.Page(onlyfiles, page)

html_app = ""
print my_page.items

for filename in enumerate(my_page.items):
    html_str = """
    <center>

    <script type = "text/javascript">
            function pic"""+str(filename[0])+"""()
            {
                document.getElementById("img").src = "images/"""+filename[1]"""";
            }
        </script>

        <img src = "" id = "img"/> <input type="button" value="File: " onclick="pic"""+str(filename[0])+"""()"/>


        <br><br>
    </center>

    """
#   <a href="./images/"""+filename[1]+""" ">"""+filename[1]+"""</a><br>

html_app+=html_str

Html_file= open("filename.html","a")
Html_file.write(html_str)
Html_file.close()
print "Done."

1 个答案:

答案 0 :(得分:1)

你在+filename[1]之后缺少一个+来连接字符串:

 "images/"""+filename[1]""" # <- missing a +

应为document.getElementById("img").src = "images/"""+filename[1]+""";"