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
#6
I meant for the open attempt on the read file to be inside the "try" section. This is a minor change:

csv_filename = "my.csv"
csv_read_counter_file = "csv_last_read"
try:
    with open(csv_read_counter_file, "r") as f:
        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))
I don't know why you would only be getting the first line. It should skip until the lines after that point are read.

I'm sure you could incorporate a range check instead of a simple "index <= lines_read" check. You would just need to determine how you want to populate the range.
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, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 207 May-03-2024, 07:23 AM
Last Post: Pedroski55
  What does .flush do? How can I change this to write to the file? Pedroski55 3 286 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Python openyxl not updating Excel file MrBean12 1 374 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 446 Feb-15-2024, 11:15 AM
Last Post: rawatg
  connect sql by python using txt. file dawid294 2 481 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,209 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Recommended way to read/create PDF file? Winfried 3 2,944 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,548 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,627 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,426 Sep-16-2023, 09:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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