问题
I wrote a simple python script like this:
#!/usr/bin/python
import sys
import urllib
if len(sys.argv) < 2:
print 'usage: python %s <file-urls>' % (sys.argv[0])
sys.exit(2)
print '%-15s %15s' % ('URL_PAGE', 'STATUS')
FileName = sys.argv[1]
InputFile = open(FileName)
for url in InputFile:
out = urllib.urlopen(url)
status = out.getcode()
print '%-15s %15s' % (url, status)
The out put is something like this:
URL_PAGE STATUS
http://test.com
200
But I want this output:
URL_PAGE STATUS
http://test.com 200
回答1:
strip
the newline character (and useless whitespace) from url
:
print '%-15s %15s' % (url.strip(), status)
来源:https://stackoverflow.com/questions/24467134/output-isnt-shown-per-line