Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop and read_lines()
#11
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
(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?
Reply
#13
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
(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
Reply
#15
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
(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.
Reply
#17
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....
Reply
#18
"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.
Reply
#19
I tried something with f.seek() and f.truncate(). It didn't work. Any suggestions?
Reply
#20
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020