EricY
EricY

Reputation: 386

Python conditional statement based on text file string

Noob question here. I'm scheduling a cron job for a Python script for every 2 hours, but I want the script to stop running after 48 hours, which is not a feature of cron. To work around this, I'm recording the number of executions at the end of the script in a text file using a tally mark x and opening the text file at the beginning of the script to only run if the count is less than n.

However, my script seems to always run regardless of the conditions. Here's an example of what I've tried:

with open("curl-output.txt", "a+") as myfile:
    data = myfile.read()

    finalrun = "xxxxx"

    if data != finalrun:
        [CURL CODE]

        with open("curl-output.txt", "a") as text_file:
            text_file.write("x")
            text_file.close()

I think I'm missing something simple here. Please advise if there is a better way of achieving this. Thanks in advance.

Upvotes: 1

Views: 3444

Answers (5)

Kalyan
Kalyan

Reputation: 43

Try using this condition along with if clause instead.

if data.count('x')==24

data string may contain extraneous data line new line characters. Check repr(data) to see if it actually a 24 x's.

Upvotes: 0

dddsnn
dddsnn

Reputation: 2481

The problem with your original code is that you're opening the file in a+ mode, which seems to set the seek position to the end of the file (try print(data) right after you read the file). If you use r instead, it works. (I'm not sure that's how it's supposed to be. This answer states it should write at the end, but read from the beginning. The documentation isn't terribly clear).

Some suggestions: Instead of comparing against the "xxxxx" string, you could just check the length of the data (if len(data) < 5). Or alternatively, as was suggested, use pickle to store a number, which might look like this:

import pickle
try:
    with open("curl-output.txt", "rb") as myfile:
        num = pickle.load(myfile)
except FileNotFoundError:
    num = 0

if num < 5:
    do_curl_stuff()
    num += 1
    with open("curl-output.txt", "wb") as myfile:
        pickle.dump(num, myfile)

Two more things concerning your original code: You're making the first with block bigger than it needs to be. Once you've read the string into data, you don't need the file object anymore, so you can remove one level of indentation from everything except data = myfile.read().

Also, you don't need to close text_file manually. with will do that for you (that's the point).

Upvotes: 2

Thomas Gak-Deluen
Thomas Gak-Deluen

Reputation: 2931

Well there probably is an end of line jump \n character which makes that your file will contain something like xx\n and not simply xx. Probably this is why your condition does not work :)

EDIT

What happens if through the python command line you type

open('filename.txt', 'r').read() # where filename is the name of your file

you will be able to see whether there is an \n or not

Upvotes: 1

11th Hour Worker
11th Hour Worker

Reputation: 379

The first bug that is immediately obvious to me is that you are appending to the file even if data == finalrun. So when data == finalrun, you don't run curl but you do append another 'x' to the file. On the next run, data will be not equal to finalrun again so it will continue to execute the curl code.

The solution is of course to nest the code that appends to the file under the if statement.

Upvotes: 1

refnode
refnode

Reputation: 11

Sounds more for a job scheduling with at command?

See http://www.ibm.com/developerworks/library/l-job-scheduling/ for different job scheduling mechanisms.

Upvotes: 1

Related Questions