在Unix下的python程序中导入文件

时间:2016-03-22 20:04:53

标签: python linux python-2.7 unix aix

Code Code下面是diff行中的打印输出。我想要 - > 200,但它在diff line中显示..

with open('URL.txt','r') as f:


    for url in f:
        print url  ## Printing URL with line gap
        ret = urllib2.urlopen(url)
        print url,"-->",ret.code

输出:

http://www.gogle.com
--> 200
http://yahoo.com --> 200

1 个答案:

答案 0 :(得分:1)

这将迭代文件的行。假设每一行都是一个网址,它会为每个网址调用你的函数。

import urllib2
with open('URL.txt','r') as f:
    for url in f:
        ret = urllib2.urlopen(url)
        print("%s --> %s" % (url.strip(), ret.code))

编辑:添加strip()以获得正确的输出