Python: Only writes last line of output

情到浓时终转凉″ 提交于 2019-12-02 11:34:05
url = "msn.com"
print("Parent:", url)
f = open("f2.txt", "w",)
for x in extractUrls(url):
    print("-", x)
    f.write( x )
f.close()

As per other answers the file write needs to be inside the loop but also try writing a new line character \n after x:

f = open("f2.txt", "w+")
for x in extractUrls(url):
    print("-", x)
    f.write( x +'\n' ) 
f.close()

Also the line return sorted(collection) if sort else collection has two indents where it should have one.

Also your subdomain code might not give what you expect for things like www.something.com.au which will only return .com.au

f = open("f2.txt", "w+", 1)

for x in extractUrls(url):
    print("-", x)
    f.write( x )

f.close()

You need to open you file then Write each X in the for loop.

At the end you can close the file.

f = open("f2.txt", "w+",1)

for x in extractUrls(url):
    print("-", x)
    f.write( x ) 

f.close()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!