Lesson 44 of 55
8 mins readPython Writing & Appending: Modes (>) and (>>)
In Plain English
Open files with mode `">"` to create or overwrite, or `">>"` to append. Direct output to the filehandle using `print $fh "text\n";`.
Deep Dive: How It Works
Write Mode (">"): Truncates existing file content and writes from beginning.
Append Mode (">>"): Preserves existing content and appends new records at EOF.
Print Syntax: `print $fh "content\n";` (note: NO comma between `$fh` and the string).
Core Rules to Remember

No Comma After $fh: Write `print $fh "msg";` — adding a comma is a syntax error.

Append Mode: Use `">>` for writing log streams.
Live Interactive Example
Hit Run Code to see it liveWriting to In-Memory Buffer
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: print $out_fh directed text directly into the filehandle target.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Write Configuration Line
Open `my $buf = "";` with `open(my $fh, ">", \$buf);`
Print `"ENV=PROD\n"` into `$fh`, then close `$fh`.
Print `"Buffer: $buf"`.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.