0

I am trying to delete a line from a text file after opening it and without storing it in any list variable using f.readlines() or anything like that. I dont have an option to open the file and store the contents in a variable and make some changes and write them to another file or any kind of operations that would require to open the file and store them again in a list variable and make some changes and store them back to the file. The file is being constantly appended by some other program, so I cannot do any kind of that stuff.

I am using f.seek() to reset the pointer to the beginning of the file, and using f.readline() as well as f.tell() to know the length of the first line. After that I am trying to replace each character with a blank space using while loop.

    pos=0
    eol = 0
    ll=0

    with open('file1.txt','rb+') as f:

        f.seek(pos,1) #position at the beginning of the file
        print(f.readline()) #reading the first line
        pos = f.tell() #storing the length of first line


    #the while loop will run from 0 to pos and replace every character with blank space
    while eol != pos:
        with open('file1.txt','rb+') as f:
            f.seek(eol,1) 
            f.write(b' ')
            eol += 1 #incrementing the eol variable to move the file pointer to next character

the code is working fine but with one problem which I cant figure out what, for example if this is the original file

file1.txt
this is line 1
this is line 2
this is line 3

after running the program , my output is

               this is line 2
this is line 3

the first line is getting deleted but there is a bunch of white space in front of the 2nd line. Maybe I am missing a simple logic here. Any help will be appreciated. Thank you

Update :

If i have understood it correctly I have changed the code and made it like this, and instead of b' ' i am putting '\r' as carraige return, which resulted in this :

the code :

while eol != pos-1:
        with open('file1.txt','rb+') as f:
            f.seek(eol,0)
            f.write(b'\r')
            eol += 1

the result :

original :
this is line 1
this is line 2
this is line 3

after execution








this is line 2
this is line 3

you see the 1st line is removed but followed with '\r'

Wan Street
  • 145
  • 1
  • 1
  • 10
  • Remember to throw in a newline somewhere. – TrebledJ Jun 12 '19 at 16:53
  • 2
    You overwrote the first line with spaces, including the end of line character, so you have as many spaces as you have printable characters + 1 more for the end of line. As there is no end of line, it appears at the beginning of the following line. Try while eol != pos-1: – Deepstop Jun 12 '19 at 16:55
  • @Deepstop's suggestion worked fine, the line is getting deleted perfectly but the next line is not coming up, its like this before delete : ` line 1 line 2 line 3 ` after delete ` <---blank space---> line 2 line 3 ` – Wan Street Jun 12 '19 at 17:20
  • @TrebledJ where to add the newline and what for? – Wan Street Jun 12 '19 at 17:21
  • 2
    Maybe I didn't make myself clear (apologies). "_the first line is getting deleted but there is a bunch of white space in front of the 2nd line._" I *hope* you realise that the whitespace is coming from `f.write(b' ')` (you've also written a comment explaining it). Line 1 (newline included) is being replaced by whitespace. So there's an off-by-one error somewhere. (My guess would be `f.seek(eol, 1)`... should that be `f.seek(eol, 0)` instead?) – TrebledJ Jun 12 '19 at 17:25
  • @TrebledJ it's fine , no worries , I am sorry it's 11:35pm here , I will have to check and report to you tomorrow. Sorry. – Wan Street Jun 12 '19 at 17:37
  • Is there anything I can use instead of white space , any Espace sequence character or anything ? – Wan Street Jun 12 '19 at 17:40
  • I think @TrebledJ is right. Instead of subtracting 1 from pos start the seek at 0. It is an offset, so the first byte will have zero offset from the start of the file. As for the other issue of seeing blank spaces at the beginning, that's what your program is doing. You could use a character instead (\f), which won't print anything as it just moves the cursor to the beginning of the line, although it is a a bit of a hack. – Deepstop Jun 12 '19 at 18:09
  • I have updated with question by adding the updated answers , so instead of using white space i am using ```'\r'``` and instead of ```f.seek(eol,1)``` i am using ```f.seek(eol,0)``` but the result is still the same.. I dont know what I am missing – Wan Street Jun 13 '19 at 12:59
  • Related: [using Python for deleting a specific line in a file](https://stackoverflow.com/questions/4710067/using-python-for-deleting-a-specific-line-in-a-file) – TrebledJ Jun 15 '19 at 01:41
  • or another solution will be to use the '\b' from certain position of the file which I can decide , for example if ```f.tell()``` is at position 50 then use the f.seek() to position the curser at lets say 25 and go to the top of the file, this will lead into a empty line at the beginning of the file but i guess I can be happy with that. But thank you so much for all the suggestions, sorry I guess I am not equipped in the head to solve this problem.. – Wan Street Jun 15 '19 at 08:18

0 Answers0