我试图读写一个文本文件。读写部分工作正常,但实际文件在程序执行后才会更新…我知道这是因为数据存储在缓冲区中,并在之后上载。因此我遇到了这个How come a file doesn't get written until I stop the program?
并尝试了.flush
,os.sync
等:但这没有影响..也许我没有看到什么。
请注意.flush
在Postdata子系统中不起作用。。。我想这是因为子程序的编码方式
Read
按预期做。
Post
获取索引和行索引,并在该位置编辑特定文本
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)
数据文件存储以下数据:
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
有没有办法实时更新文件,这样我就可以调用代码的其他部分来读取文件中的数据…我将使用类似getch
的东西来运行其他东西…我不介意在读取时暂停postng数据。。。我试着做第二个文件,数据例如:filex从Getdata()
中读取,post数据首先写入filey
,然后将所有内容复制到filex
,但这也不起作用。
如果有帮助的话,文本文件中可能会有大约10-50行
目前没有回答
相关问题 更多 >
编程相关推荐