Python Forum
how to change the range of read CSV file every time python file runs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to change the range of read CSV file every time python file runs
#4
I thought you meant that you wanted to read the rest of the CSV later in the same script.

I see now that you just want to read the "new" stuff each time it runs. Yes, you'd have to keep state somewhere like the file offset or the number of lines previously read.

This partial solution can have problems if it crashes or is run simultaneously (no locking, no error checking, etc.)

csv_filename = "my.csv"
csv_read_counter_file = "csv_last_read"
with open(csv_read_counter_file, "r") as f:
    try:
        lines_read = int(f.readline().rstrip())
    except:
        # On any error, just reset back to start of file.
        lines_read = 0

with open(csv_filename, "r") as f:
    for index, line in enumerate(f):
        if index < lines_read:
            continue
        print(line.rstrip())

with open(csv_read_counter_file, "w") as f:
    f.write(str(index))
Reply


Messages In This Thread
RE: how to change the range of read CSV file every time python file runs - by bowlofred - Dec-08-2020, 08:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 233 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Python openyxl not updating Excel file MrBean12 1 344 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 418 Feb-15-2024, 11:15 AM
Last Post: rawatg
  connect sql by python using txt. file dawid294 2 445 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,159 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Recommended way to read/create PDF file? Winfried 3 2,902 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,475 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,471 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,382 Sep-16-2023, 09:46 PM
Last Post: snippsat
  How to do 100 runs simulation based on the current codes? dududada 6 999 Sep-03-2023, 01:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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