Menu

1 / 2

Python os Module: File System Operations Every Script Needs

DEV Community·German Yamil·6 months ago
#OAug1X9R
#python#ospath#beginners#tutorial#path
Reading 0:00
15s threshold

Python os Module: File System Operations Every Script Needs

Every automation script eventually needs to touch the file system — create a directory, check if a file exists, read a config value from the environment.

The os module is Python's interface to the operating system. No shell required.

🎁 Free: AI Publishing Checklist — 7 steps in Python · Full pipeline: germy5.gumroad.com/l/xhxkzz (pay what you want, min $9.99)

What os gives you

import os

# Where am I right now? print ( os . getcwd ()) # /Users/yamil/projects/pipeline # What's in this directory? print ( os . listdir ( " . " )) # ['main.py', 'state.json', 'chapters'] # Does this path exist? print ( os . path . exists ( " state.json " )) # True or False

Enter fullscreen mode

Exit fullscreen mode

Three things you can do without touching the shell, without subprocess , and without any external packages. os ships with Python and works the same on macOS, Linux, and Windows.

os.path — the building blocks

os.path handles path string manipulation.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.