简体   繁体   中英

Read file letter by letter and transfer the result to another file in python

I want to read this file letter by letter and transfer the result to another file.

This is my Try, but it did not work In transferring the result to the file (f2)

with open("Text.txt") as f:
while True:
    letter = f.read(1)
    with open("Output.txt",'w') as f2:
        f2.write(letter)
    print (" The letter is :", letter)# This just for try if work or not ...and his print the result in consol
    if not letter:
        print ("End Of File")
        break

    f2.close()

the file Text.txt have text of code source

Please help me and thank you for your support.

Yes, after several failed try, I solved it

with open("Text.txt",'r') as f:
letter = ""
l = []
while True:
    letter = f.read(1)
    l.append(letter)
    f2 = open("Output.txt",'w')

    f2.write(str(' '.join(l)))
    print (" The letter is :", letter)
    if not letter:
        print ("End Of File")
        break

Thanks to everyone who tried to help me ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM