0

I have a working function using loop, that works fine, in the example code I have, there are 4 CSV files, but it could go to 40 or more CSV files.

  • this codes reads from first CSV file (file1.csv), loop from a range( in this example starts from row 0 to 3),
  • use this data from the rows, (one at the time) to run the function,
  • then it goes to the second CSV file and uses the data from same range(0-3), until the last CSV file.

I want to run this Python file on schedule (once,twice, or more daily), so I want to add another loop that when next time Python file runs, it reads CSV file from where it was left off. (row 3 to 6 in this example) for all CSV files, again the next time it runs, it starts from where it was left off ( row 6 t 9 in this example) and so on for all CSV files., of course this range is an example,

I am not sure if this is doable or not, I can not figure it out how to do that, appreciate your help.

here is the code I have:

from abc.zzz  import xyz
path_id_map = [
    {'path':'file1.csv', 'id': '12345678'},
    {'path':'file2.csv', 'id': '44556677'}
    {'path':'file3.csv', 'id': '33377799'}
    {'path':'file4.csv', 'id': '66221144'}]
s_id = None
for pair in path_id_map:
    with open(pair['path'], 'r') as f:
        for i in range(0, 3):      
            zzz.func1(id_1=f.readline().rstrip(), B_id=pair['id'], s_id=s_id)
            time.sleep(25)
Dave99
  • 95
  • 8
  • 3
    You will have to store the information about where your current position is somewhere (in a text file, for example), read it each time you start and update it when leaving. – Thierry Lathuille Dec 08 '20 at 11:33
  • Does this answer your question? [Python script to do something at the same time every day](https://stackoverflow.com/questions/15088037/python-script-to-do-something-at-the-same-time-every-day) – Tomerikoo Dec 08 '20 at 11:34
  • @Tomerikoo thanks for the reply, the link is for schedule only, my problem is the nested loop – Dave99 Dec 08 '20 at 11:37
  • Then focus your question on that with a [mre] of how you reschedule. Right now you are basically asking two questions in one which is off-topic – Tomerikoo Dec 08 '20 at 11:44
  • @Tomerikoo, thanks for the link, I am not asking 2 questions, the title is not about schedule and nested loop, I mentioned schedule in my post just to make it clear why I need nested loop, – Dave99 Dec 08 '20 at 12:20
  • In that case your question is just not clear... Do you mean something like `for i in range(x, x+3)` and then doing `x += 3`? – Tomerikoo Dec 08 '20 at 12:24
  • @Tomerikoo, thanks that is a good idea, I will try to see if I can come up with a working code, I was working on what Thierry said, – Dave99 Dec 08 '20 at 12:33

2 Answers2

1
  • If you want to remember the exeution state of a program, you need to store any of these variables somewhere. In the example below I've used a plain and simple text file, but it could be a database or any other form of persistent storage.

  • To help make things easier to change and adapt, you should break to program down into small functions that accomplish one task.

def read_range(pair, start_line, end_line):
    with open(pair['path'], 'r') as f:
        line_counter = 0
        for line in f:            
            if line_counter >= start_line and line_counter < end_line:
                zzz.func1(id_1=f.readline().rstrip(), B_id=pair['id'], s_id=s_id)
                time.sleep(25)
            elif line_counter >= end_line:
                break
            line_counter = line_counter + 1

def read_start_number():
    try:
        lines = open("save.txt", 'r').readlines()
    except FileNotFoundError:
        write_start_number(0)
        lines = read_start_number()
    return lines[0]
    

def write_start_number(line_number):
    with open('save.txt', 'w') as f:
        f.write(str(line_number))
        
path_id_map = [
    {'path':'file1.csv', 'id': '12345678'},
    {'path':'file2.csv', 'id': '44556677'}
    {'path':'file3.csv', 'id': '33377799'}
    {'path':'file4.csv', 'id': '66221144'}]
s_id = None

start_number = int(read_start_number())
print(start_number)
for pair in path_id_map:
    read_range(pair, start_number, start_number + 3)
write_start_number(start_number+3)
el_oso
  • 1,021
  • 6
  • 10
0

You could create a function to run your code, and use variables to set the range it has to parse. Each time you call the function, you update the variable (adding +3 in your example) and pass them in the function. You can update those variables with more complex rules, as adding till a certain number, then restart to 0, etc.

Jacques
  • 11
  • 4
  • I don't see how that answers the question as you basically described what OP needs... The question is exactly how to do everything you described in your answer... – Tomerikoo Dec 08 '20 at 11:45
  • Well, OP seems to use static numbers for the parsing range, and not using the function so he can pass arguments. Guessed it was his problem, as the sample code is not comprehensive. – Jacques Dec 08 '20 at 11:48