Python Forum
python one line file processing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python one line file processing
#4
You can process the first time the whole file and use after the iteration of lines the method tell of of the file object, which tells you where you are (at which byte). You can convert the integer to a str and write it to a file. Next time the script looks for this file and if the file is present, it should load the content of the file, convert it back to an int and you use before you start iterating over the lines, you use seek(position) on the file object. Then you have the position, where your script finished last time.

In [20]: with open('birds.txt') as fd: 
    ...:     for line in fd: 
    ...:         print(line.strip()) 
    ...:     print(fd.tell()) 
    ...: #fd.tell() <- file is already closed 
    ...:                                                                                                                                                                                    
2010-01-01 01:01:00.0000 left
2010-11-01 01:01:00.0000 right
2010-10-01 01:01:00.0000 right
91
So, if a program writes now to birds.txt, it starts as byte position 91.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
python one line file processing - by har - Dec-05-2019, 09:16 AM
RE: python one line file processing - by ichabod801 - Dec-05-2019, 03:36 PM
RE: python one line file processing - by har - Dec-06-2019, 01:30 AM
RE: python one line file processing - by DeaD_EyE - Dec-06-2019, 08:16 AM
RE: python one line file processing - by har - Dec-09-2019, 06:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,598 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 878 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,395 Sep-27-2022, 01:38 PM
Last Post: buran
  Print to a New Line when Appending File DaveG 0 1,232 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,518 Mar-18-2022, 07:49 PM
Last Post: snippsat
  CSV to Text File and write a line in newline atomxkai 4 2,716 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,416 Feb-13-2022, 07:13 PM
Last Post: menator01
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 5,643 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  multi-line CMD in one-line python kucingkembar 5 4,002 Jan-01-2022, 12:45 PM
Last Post: kucingkembar
  writelines only writes one line to file gr3yali3n 2 2,394 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n

Forum Jump:

User Panel Messages

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