语句迭代中没有出现换行符

Ast*_*ron 0 python

以下内容从表单中获取多个文件输入,并以串行方式写入.虽然当每个文件成功登录页面时它会打印,但每个循环之间没有换行符?解决这个问题的最佳方法是什么?我以为print语句会默认添加换行符?

#!/usr/bin/python
import cgi, os
import shutil
import cgitb; cgitb.enable()  # for troubleshooting

form = cgi.FieldStorage()

print """\
Content-Type: text/html\n
<html><body>
"""

if 'file' in form:
   filefield = form['file']
   if not isinstance(filefield, list):
      filefield = [filefield]

   for fileitem in filefield:
       if fileitem.filename:
          fn = os.path.basename(fileitem.filename)
          # save file
          with open('/var/www/rsreese.com/files/' + fn, 'wb') as f:
              shutil.copyfileobj(fileitem.file, f)
          # line breaks are not occuring between interations
          print 'File "' + fn + '" was uploaded successfully \n'
          message = 'All files uploaded'
else:
   message = 'No file was uploaded'

print """\
<p>%s</p>
</body></html>
""" % (message)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 6

Python会很好地打印换行符,但是你的浏览器不会显示这些换行符.

<br/>改为使用标签,或将整个输出包装在<pre>/ </pre>tags中.