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

3
  • You have an indentation problem, one space was missing at the beginning of fonlyOnce .... Have edited it. Commented Apr 14, 2016 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? Commented Apr 14, 2016 at 17:10
  • Possible duplicate of Fastest Way to Delete a Line from Large File in Python Commented Apr 14, 2016 at 17:35

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

6 Comments

Okay, but if i want always to read from the "onlyOnce.txt" file ,, the update data saved in NewFile !!
@Samah yes it is saved to new file
then !! if i want to overwrite !!! i want to rewrite on the same file because i want to read from it only
@Samah if you want to overwrite original file, just change the new file name to original file name
I edited your code , but i still have error :Traceback (most recent call last): File "<encoding error>", line 26, in <module> ValueError: not enough values to unpack (expected 3, got 1):
|
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.