Search code examples
pythoncmdprinting

print with carriage return not working, printing new lines


I have a bit of python code that is used to "clean up" a keylogger log, (educational purposes) and this is how it goes. I wanted to make it look cool by displaying the changes every time it gets changed and wanted to do it like this;

Helloo[bkspace] => then gets replaced by Hello.

The code for this is working but the print results in new lines.

def readfile_keylog(filein, fileout):
    fi = open(filein, "r")
    fo = open(fileout, "a")

    for line in fi:
        while "[bkspace]" in line:
            index = line.index("[bkspace]")
            line = line.replace("[bkspace]", "", 1)
            line = line[:index - 1] + line[index:]
            print("\r", str(line), end="", flush=True)

        fo.write(line)


readfile_keylog("login.txt", "out.txt")

I even tried using sys.stdout.write and flushing and it didnt work. when I tried simple code like this it works fine

for i in range(10):
  print("\r", i, end="", flush=True)
  time.sleep(0.1)

this is how the output looks, obviously not my intention.

 i feel like if you have it all the time the same people would be jki[bkspace][bkspace][bkspace]kinda forced to rush the [bkspace] ci[bkspace][bkspace]10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be jk[bkspace][bkspace]kinda forced to rush the [bkspace] ci[bkspace][bkspace]10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be j[bkspace]kinda forced to rush the [bkspace] ci[bkspace][bkspace]10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the [bkspace] ci[bkspace][bkspace]10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the ci[bkspace][bkspace]10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the c[bkspace]10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the 10[bkspace][bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the 1[bkspace]civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the civilwear[bkspace][bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the civilwea[bkspace][bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the civilwe[bkspace]ars early 
 i feel like if you have it all the time the same people would be kinda forced to rush the civilwars early 

The results look like this in terminal and pycharm.


Solution

  • I found the issue. Its taking lines from the file. the lines contain a new line at the end. just needed to remove that with a simple if, anyways, i opted for a different way by clearing the console.

    heres how it looks now, quite satisfying https://streamable.com/rtkg2p