zwep
zwep

Reputation: 1340

Writing data with python script and cron

I have already seen a lot of questions around this.. and a lot of proposed solutions. But none have worked for me so far. So here we go...

A simple python script called test.py:

#!/usr/bin/env python3


from datetime import datetime
print('Im alive')
fn = 'msgs.txt'                                                                 
with open('/home/username/Documents/code/production/msgs.txt', 'aw') as f:
    f.write('%s\n' % datetime.now())

And here the line in the sudo crontab -e file

*/1 * * * * /home/username/Documents/code/production/test.py >> /home/username/outputlog.txt

The log shows that the program runs and executes properly, I have used full paths in specifying the file that Im writing too, stated the job in sudo crontab in case anything goes wrong with the user... and at this point I am lost. I don't know what to change anymore and all others questions that Ihave seen wont help me further.

Anyone else has another idea here?

Upvotes: 0

Views: 20

Answers (1)

hemor
hemor

Reputation: 51

Short Answer: Change 'aw' file open mode to 'a'

With your current code, you'll get the error

ValueError: must have exactly one of create/read/write/append mode

because you are trying to combine append ('a') and write ('w') modes. Simply using append ('a') mode fixes the problem.

Upvotes: 1

Related Questions