Append to an Audit Trail

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.7
An audit trail must never lose history. Write log_action(path, user, action) that appends a line formatted as user|action to the file, creating it if it doesn't exist. Explain-by-code why mode 'a' and not 'w'.

Examples

Example 1
Input
log_action('audit.txt', 'rahul', 'login')
Output
file gains the line 'rahul|login'
Explanation

Mode 'a' preserves existing lines and adds to the end.

Constraints

  • Use mode 'a'.
  • Line format: user|action with a trailing newline.

Topics

File Handlingappend mode

Companies

JPMorgan ChaseDeloitte

Hints

Hint 1

'a' creates the file when missing, appends when present.

Hint 2

An f-string builds the line.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.