0
def readfile(filename,mode="rt"):
    head = "<li><a href="""
    center =""" target="_blank">"""
    end ="<br></a></li>"
    with open(filename,mode) as fin:
        for line in fin:
        link = line
        length_link = len(link)
        twitter_name_after = link[twitter_name_before+5:]
        full_code = head+link+center+"@"+twitter_name_after+end
        writefile("C:/Users/Johny The Overlord/Documents/Code/test_1.txt",full_code)

def writefile(filename,contents,mode="wt"):
        with open(filename,mode) as fout:
            fout.write(contents)
def main():
        # my code here
        readfile("C:/Users/Johny The Overlord/Documents/Code/test.txt")
if __name__ == "__main__":
        main()

url in text1 https://twitter.com/xxx https://twitter.com/yyy https://twitter.com/zzz

in text2 should have

  • https://twitter.com/xxx target="_blank">@xxx
  • https://twitter.com/yyy target="_blank">@yyy
  • https://twitter.com/zzz target="_blank">@zzz
  • but it only write last url. how do I fix?

    J. Sumittanun
    • 87
    • 1
    • 7

    2 Answers2

    1

    I think it is just because of writing mode. Try using "a" or"a+" to amend have a look here https://stackoverflow.com/a/23051095/3759158

    Manishh
    • 1,444
    • 13
    • 23
    0

    You can overwrite the previous one, try to write it (i mean in write function) in separate lines.

    Add fout.write("\n") to your write functions after fout.write(contents)

    Nove124
    • 223
    • 2
    • 6