Posts: 8,168
Threads: 160
Joined: Sep 2016
I don't have Quora account and don't want to create one
your waits have timeout of 10sec or until element is available, whichever comes first. You may want to increase the timeout if it fails sometime.
same for sleep()
Posts: 15
Threads: 2
Joined: Feb 2019
(Feb-05-2019, 09:32 AM)buran Wrote: I don't have Quora account and don't want to create one
your waits have timeout of 10sec or until element is available, whichever comes first. You may want to increase the timeout if it fails sometime.
same for sleep()
Hey,
Thank you. The code seems to work. Fails sometimes but works mostly.
One more question. How can I make the loop after it reads each line, does it what its supposed to do, and then delete that read line and move onto the next?
For example if there are
Line 1
Line 2
Line 3
...
After it reads line 1, the function does something. I want it to delete Line 1 and move onto line 2 in the next loop, the function does something then deletes line 2 and moves onto line 3....
Any ideas?
Posts: 8,168
Threads: 160
Joined: Sep 2016
Feb-06-2019, 10:05 AM
(This post was last modified: Feb-06-2019, 10:05 AM by buran.)
in the loop you iterate over the lines in the file one by one. You cannot remove the lines from the file, while iterating over it.
you can either keep track of the lines you have already processed and (assuming somehow you break out of the loop before end of file) remove them later - read the file and save to different file only those lines that were not processed.
as an alternative you can read the entire file in memory with readlines. you will get list of all lines. loop over the list and remove lines already processed. if you break out before list is exhausted - overwrite the old file with the remaining lines from the list.
Posts: 15
Threads: 2
Joined: Feb 2019
(Feb-06-2019, 10:05 AM)buran Wrote: in the loop you iterate over the lines in the file one by one. You cannot remove the lines from the file, while iterating over it.
you can either keep track of the lines you have already processed and (assuming somehow you break out of the loop before end of file) remove them later - read the file and save to different file only those lines that were not processed.
as an alternative you can read the entire file in memory with readlines. you will get list of all lines. loop over the list and remove lines already processed. if you break out before list is exhausted - overwrite the old file with the remaining lines from the list.
Hi,
I've tried this
#question = []
#with open("test.txt", "r+") as f:
#for line in f:
#question.append(line)
#print ("successfully added " + line)
#question.split([1:]) but it doesn't work. Where's problem?
I can understand, almost, what you are trying to say in English but I cannot convert it to Python
Posts: 8,168
Threads: 160
Joined: Sep 2016
Let me first ask question - is it feasible that you may stop processing the file before you finish it? Usually you will open a file and process all lines, till you reach the end. My question is, assuming that you have 100 lines in the file, is it possible that you stop after only 20 lines were processed. If I understand correctly, in this case you want to remove these 20 lines.
Posts: 15
Threads: 2
Joined: Feb 2019
(Feb-07-2019, 09:42 AM)buran Wrote: Let me first ask question - is it feasible that you may stop processing the file before you finish it? Usually you will open a file and process all lines, till you reach the end. My question is, assuming that you have 100 lines in the file, is it possible that you stop after only 20 lines were processed. If I understand correctly, in this case you want to remove these 20 lines.
The whole point is that the script should run, process as many number of lines it can, until an error shows up or a timeout. First loop be it 10 next 20, I don't know a 100. The number doesn't matter but during each loop, I want it to run, process the line, delete it and move on.
Posts: 15
Threads: 2
Joined: Feb 2019
How can I make the loop after it reads each line, does it what its supposed to do, and then delete that read line and move onto the next?
For example if there are
Line 1
Line 2
Line 3
...
After it reads line 1, the function does something. I want it to delete Line 1 and move onto line 2 in the next loop, the function does something then deletes line 2 and moves onto line 3....
Posts: 1,950
Threads: 8
Joined: Jun 2018
"If fact, in any programming language for most part if you mutate something while you iterating over it you living in state of sin and you deserve whatever happens to you" -- Raymond Hettinger, Python core-developer, Transforming Code into Beautiful, Idiomatic Python
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 15
Threads: 2
Joined: Feb 2019
I tried something with f.seek() and f.truncate(). It didn't work. Any suggestions?
Posts: 8,168
Threads: 160
Joined: Sep 2016
Feb-08-2019, 12:26 PM
(This post was last modified: Feb-08-2019, 12:27 PM by buran.)
I have merged the threads, otherwise it may become confusing to follow parallel discussion.
As already explained - you cannot change the file while iterating over it. For the purpose you describe, using text files - just keep track of processed lines in separate file, in case of unexpected error - clean the original file from lines already processed before you start again.
In more advanced approach - import the original information in a database (e.g. sqlite). Have a special flag if and when the record has been processed. Keep info up to date by updating each record immediately after it has been processed.
|