1

I am trying to read and write to a text file . The reading and writing parts works fine but the actual file does not update until after the program has executed...I understand that this is because the data is being stored in a buffer and is being uploaded after.so I came across this How come a file doesn't get written until I stop the program? and tried the .flush, os.sync, etc: this did not have an affect though..maybe I'm not seeing something. Note that the .flush does not work in the Postdata sub ... I think it's because of the way that subroutine is coded.

Read does as it is expected. Post takes an index and a line index and edits that specific text at that position.

def Getdata(Index,lineindex):#indexed so can say get data at index 3 and it will return it
    Datafile = open("Trade data/3rd file", "a+")
    Linetoget = linecache.getline('Trade data/Databaseforbot', lineindex).split("|")
    Traddetail = Linetoget[Index]
    print(Traddetail)

    return Traddetail

def Postdata(index,lineindex,data):#will work fine the first time, but run it as PostdataV1(3,2) it will convert
    Getdata(3,2)
    with fileinput.FileInput('Trade data/Databaseforbot', inplace=True, backup='.bak') as file:
        entireline= linecache.getline('Trade data/Databaseforbot', lineindex)
        splitted = entireline.split("|")
        Traddetail = splitted[index]
        Newline = entireline.replace(Traddetail, str(index+1)+"*"+data)
        for line in file:
            print(line.replace(entireline, Newline), end='')

            #os.fsync(file)

        file.close()
Getdata(3, 2)
Postdata(3,2,"QW")
Getdata(3, 2)

The data file stores this data:

1|https://app.libertex.com/products/stock/BA/|3*45#4|4*0|5*0|6*0|7*0|8*0|9*Up|CDwindow-5C5C0883A51583A013B50FDC5A1798B7
2|https://app.libertex.com/products/energetics/NG/|3*56#5|4*0|5*0|6*0|7*0|8*0|9*Up|CDwindow-5C5C0883A51583A013B50FDC5A1798B7
3|https://app.libertex.com/products/metal/XAUUSD/|3*45#4|4*0|5*0|6*0|7*0|8*0|9*Up|CDwindow-5C5C0883A51583A013

Is there a way to live update the file so I can call other parts of the code to read the data from the file...I will be using something like getch to run other stuff...I don't mind if I have to pause postng data while reading... I tried doing a second file that the data eg: filex which is read from in Getdata() and the post data first writes to filey then copy everything to filex, but that did not work either. Also there wil be around maybe 10-50 lines in the text file, if that helps.

Almog
  • 452
  • 1
  • 7
  • 13
Hamza Arshad
  • 121
  • 1
  • 3
  • 11
  • 4
    You don't need to use `file.close()` when you use `with`, the context manager automatically closes it. – Barmar Aug 22 '20 at 17:54
  • 1
    Use `file.flush()` and remove `file.close()` as `with` will do it for you. The flush operation forces the file data into the fileThe flush operation forces the file data into the file. – JRodDynamite Aug 22 '20 at 17:55
  • @JRodDynamite should `file.flush()` be inside or after `with` ? – RichieV Aug 22 '20 at 18:04
  • 1
    @RichieV - It should be inside `with`. The file is closed outside the `with` block. – JRodDynamite Aug 22 '20 at 18:06
  • 1
    i understand that the file.flush should work but it wont work insinde of the with block .as in it does not come up in the suggested code part and . it returns AttributeError: 'FileInput' object has no attribute 'flush', if you run it – Hamza Arshad Aug 22 '20 at 18:13
  • the flush does work in the Getdata.. do you think if i call the getdata within the post data it will flush or not? – Hamza Arshad Aug 22 '20 at 18:14
  • 1
    I don't understand why you want to save in file and read it again - better keep it in memory - it should works faster. Do you run it with `threading` or `multiprocessing` ? It can make problem - some function may try to read data before other function even start writing. Frankly, I wouldn't use `FileInput` to change data in file but I would only read from original file and save changes to different file. And even instead of using file I would use database `sqlite` – furas Aug 22 '20 at 18:58
  • im scared to make a database and this bug has made it offically the better choice lol. – Hamza Arshad Aug 22 '20 at 19:13

0 Answers0