Python: Only writes last line of output

后端 未结 4 1978
耶瑟儿~
耶瑟儿~ 2021-01-29 01:05

Trying to write a program that extracts URLs from a website. The output is good, but when I try to write the output to a file, only the last record is written. Here is the code:

相关标签:
4条回答
  • 2021-01-29 01:47
    f = open("f2.txt", "w+", 1)
    
    for x in extractUrls(url):
        print("-", x)
        f.write( x )
    
    f.close()
    
    0 讨论(0)
  • 2021-01-29 01:56

    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

    0 讨论(0)
  • 2021-01-29 01:57
    url = "msn.com"
    print("Parent:", url)
    f = open("f2.txt", "w",)
    for x in extractUrls(url):
        print("-", x)
        f.write( x )
    f.close()
    
    0 讨论(0)
  • 2021-01-29 02:01

    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()
    
    0 讨论(0)
提交回复
热议问题