0

The code is:

from datetime import datetime,time
from csv import reader

with open('onlyOnce.txt', 'r+') as fonlyOnce:
    for f_time, sec_time, dte in filter(None, reader(fonlyOnce, delimiter="_")):

        check_stime=f_time.split(":")
        Stask_hour=check_stime[0]
        Stask_minutes=check_stime[1]
        check_stime = datetime.strptime(f_time,"%H:%m").time()

        check_etime=sec_time.split(":")
        Etask_hour=check_etime[0]
        Etask_minutes=check_etime[1]

        #check every minute if current information = desired information
        now = datetime.now()
        now_time = now.time()
        date_now = now.date()

        if (date_now.strftime("%Y-%m-%d") == dte and time(int(Stask_hour),int(Stask_minutes)) <= now_time <= time(int(Etask_hour),int(Etask_minutes))):
            print("this line in range time: "+ f_time)
            #delete this line
            fonlyOnce.write(" ")
        else:
            print("Padraic Cunningham")
fonlyOnce.close()

The goal of this code is to :

1- loop on the lines in the file

2- check if any line it in the range of current time

3- if yes: print this line in range time: 9:1 and delete this line from the same file.

4-data in the file is:

7:1_8:35_2016-04-14
8:1_9:35_2016-04-14
9:1_10:35_2016-04-14

5- output must be:

7:1_8:35_2016-04-14
8:1_9:35_2016-04-14

because the last line has the time in range of current time.it must delete and replace empty line.

My problem is this code will clean all file and i don't want that:

invaild code: fonlyOnce.write(" ")

Thanks

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
Samah
  • 61
  • 1
  • 10
  • You have an indentation problem, one space was missing at the beginning of fonlyOnce .... Have edited it. – tfv Apr 14 '16 at 17:07
  • I think you're over complicating this by trying to do it in-place. Have you tried writing out the lines you want to keep to a temporary file, and then replacing the original file with the temporary file at the end? – SpoonMeiser Apr 14 '16 at 17:10
  • Possible duplicate of [Fastest Way to Delete a Line from Large File in Python](http://stackoverflow.com/questions/2329417/fastest-way-to-delete-a-line-from-large-file-in-python) – martijnn2008 Apr 14 '16 at 17:35

3 Answers3

1

what I did:
1. remove determining function out of loop.
2. if not fit your needs, replace data with empty list
3. open a new file to write processed data

    def IsFit( f_time, sec_time, dte ):
        check_stime=f_time.split(":")
        Stask_hour=check_stime[0]
        Stask_minutes=check_stime[1]
        check_stime = datetime.strptime(f_time,"%H:%m").time()

        check_etime=sec_time.split(":")
        Etask_hour=check_etime[0]
        Etask_minutes=check_etime[1]

        #check every minute if current information = desired information
        now = datetime.now()
        now_time = now.time()
        date_now = now.date()

        if (date_now.strftime("%Y-%m-%d") == dte and time(int(Stask_hour),int(Stask_minutes)) <= now_time <= time(int(Etask_hour),int(Etask_minutes))):
            return False
        else:
            return True

    with open('onlyOnce.txt', 'r+') as fonlyOnce:
        res = [  line if IsFit(*line ) else [] for line in csv.reader(fonlyOnce, delimiter="_") if line ]

    with open(NewFile,'wb') as f:
        wirter = csv.wither(f)
        wirter.writerows(res)
galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • Okay, but if i want always to read from the "onlyOnce.txt" file ,, the update data saved in NewFile !! – Samah Apr 14 '16 at 18:07
  • @Samah yes it is saved to new file – galaxyan Apr 14 '16 at 18:08
  • then !! if i want to overwrite !!! i want to rewrite on the same file because i want to read from it only – Samah Apr 14 '16 at 18:10
  • @Samah if you want to overwrite original file, just change the new file name to original file name – galaxyan Apr 14 '16 at 18:11
  • I edited your code , but i still have error :Traceback (most recent call last): File "", line 26, in ValueError: not enough values to unpack (expected 3, got 1): – Samah Apr 14 '16 at 18:23
  • @Samah edited.I don't know the format of you file. I assume the each line of data comes with 3 elements – galaxyan Apr 14 '16 at 18:44
0

Brute-force solution - suitable for small size files

0- create a buffer of lines

1- loop on the lines in the file

1.1- check if any line it in the range of current time

1.2- if yes: print this line in range time: 9:1 if no: add the line into the buffer

2- close the file for read

3- add an empty line into the buffer

4- reopen the file for write

5- flush the buffer into the file, and save the file

0
  • You do not want to edit a file you are reading. This is a bad idea!

  • Instead, you might want to consider reading each line of the file into a list, deleting unwanted items from the list, then writing over the file with this list.

    If your file is large, this might be slow, however.

    Also, at the end of your code you call fonlyOnce.close(), but you don't need to. The context manager (the with statement) automatically closes the file once you leave it.

lochsh
  • 366
  • 1
  • 12