Reputation: 3
I have list of big .txt files. To save them, I am making a for loop, but the problem is that the final text is cutted when it is saved(it is not the full file), I assume, because Python is moving too fast and don't have time to save everything, because when I try to print the text on the console it is in the script, just not saving. I have added time.sleep()
, and with 3 or 5 sec everything is saved in the txt, but I was wondering for better way, something like "wait until", to wait the saving. Because I am not sure on a different computers what time will take, or with different .txt files.Thanks in advance.
Code:
Text=[Text1, Text2, Text3, Text4]
index=0
for x in List_Of_Names:
Name=str(List_Of_Names[index])
file=open('Texts\\'+Product+'_'+ Name + '.txt', 'w')
file.write(Texts[index])
file.close
time.sleep(5)
index+=1
time.sleep(5)
print('The new .txt files were saved in ' + Location +"\\Scenes" )
print('\n')
print('Press "Enter" to escape.')
wait=sys.stdin.readline()
Upvotes: 0
Views: 47
Reputation: 2634
Use with
for opening the file as:-
with open('Texts\\'+Product+'_'+ Name + '.txt', 'w') as fd:
fd.write(Texts[index])
Upvotes: -1
Reputation: 82765
Looks like you are re-writing the content of your text with each iteration.
Try:
with open('Texts\\'+Product+'_'+ Name + '.txt', "w") as infile:
for x in List_Of_Names:
ViewName=str(List_Of_Names[index])
infile.write(Texts[index])
Upvotes: 2