0

下面从一个表单中获取多个文件输入,并以串行方法将它们写入。虽然它确实在每个文件成功到页面时打印,但每个循环之间没有发生换行符?解决此问题的最佳方法是什么?我认为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)
4

1 回答 1

6

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

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

于 2012-09-03T13:16:54.697 回答