Bird
0
0

What will be the output of this Python script scheduled as a cron job on Raspberry Pi?

medium📝 Predict Output Q13 of 15
Raspberry Pi - Automation and Scheduling

What will be the output of this Python script scheduled as a cron job on Raspberry Pi?

import datetime
with open('/home/pi/log.txt', 'a') as f:
    f.write(f"Run at {datetime.datetime.now()}\n")

Assuming the cron job runs every minute, what happens?

AThe file <code>log.txt</code> will have a new timestamp line added every minute
BThe file <code>log.txt</code> will be overwritten every minute with one timestamp
CThe script will cause an error and stop running
DNothing will happen because cron jobs cannot run Python scripts
Step-by-Step Solution
Solution:
  1. Step 1: Understand the script behavior

    The script opens log.txt in append mode ('a'), so it adds lines without deleting old ones.
  2. Step 2: Consider the cron schedule

    Since the cron runs every minute, the script adds a new timestamp line every minute.
  3. Final Answer:

    The file log.txt will have a new timestamp line added every minute -> Option A
  4. Quick Check:

    Append mode adds lines each run = A [OK]
Quick Trick: Append mode 'a' adds lines, doesn't overwrite [OK]
Common Mistakes:
MISTAKES
  • Thinking the file is overwritten (confusing 'w' and 'a')
  • Assuming cron can't run Python scripts
  • Expecting an error without checking code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes