Lesson 45 of 55
9 mins readPython File Test Operators & Directory Operations
In Plain English
Perl includes UNIX file test operators: `-e` (exists), `-f` (plain file), `-d` (directory), `-s` (file size), and `glob("*.pl")` for filename pattern matching.
Deep Dive: How It Works
File Tests: `-e $path` (exists), `-f $path` (is file), `-d $path` (is directory), `-z $path` (is empty), `-s $path` (returns size in bytes).
Stacked Tests: `-f -r $path` tests if a file is both a plain file and readable.
Directory Reading: `opendir`, `readdir`, `closedir` or wildcard `glob("dir/*")`.
Core Rules to Remember

Single-Character Test Operators: `-e`, `-f`, `-d`, `-s` provide instant filesystem attribute checks.

glob() Wildcards: Quickly find files matching path patterns.
Live Interactive Example
Hit Run Code to see it liveTesting Path Types and Size
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: File test operators identified the file, directory, and missing path.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Inspect Target Path
Check `/dev/null` using `if (-e "/dev/null")` and print `"System device present\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.