The os
module in Python provides functions to interact with the operating system (OS).
It allows you to work with file systems, environment variables, process management, and system commands.
Path and File Operations
os.getcwd()
Linux uses slashes (/
) as path separators.
Windows uses backslashes (\
) as path separators. Since Python understands forward slashes (/
) even on Windows, use slashes(/
) for os.path
related operations.
OS | os.getcwd() | os.getcwd().replace(“\”, “/”) |
---|
Windows | C:\Users\Username\projects | /home/username/projects |
Linux | /home/username/projects | /home/username/projects |
1
2
3
4
5
6
7
8
9
10
| import os
# Linux
path = os.getcwd()
print(path) # /home/username/projects
# Windows
path = os.getcwd()
print(path) # C:\Users\Username\projects
print(path.replace("\\", "/")) # C:/Users/Username/projects
|
os.path.exists()
Check if a File or Directory Exists.
1
2
3
4
5
6
7
8
| import os
# Linux
print(os.path.exists("/home/user/projects/data.txt")) # Returns True or False
# Windows
# As I told you, Python understands forward slashes (`/`) even on Windows
print(os.path.exists("C:\Users\Username\projects/data.txt")) # Returns True or False
|
os.path.isfile() and os.path.isdir()
Check if it’s a File or a Directory.
1
2
3
4
| import os
print(os.path.isfile("/home/user/projects/data.txt")) # True if it's a file
print(os.path.isdir("/home/user/projects/data")) # True if it's a directory
|
os.listdir()
List Files in a Directory.
1
2
3
| import os
print(os.listdir("/home/user/projects")) # Returns a list of files and folders
|
1
| ['.dockerignore', '.env', '.flake8', '.gitignore', 'commons', 'config', 'dockerfile', 'dockertest_main.py', 'log', 'pretrained_models', 'readme.md', 'requirements.txt', 'tasks', 'zMinioTest.txt', 'zMyTest.ipynb']
|
os.makedirs()
Create a Directory. For reference, os.mkdir
is also available, but it cannot create nested directories. If possible, use os.makedirs
instead.
The exist_ok=True
argument is used in functions like os.makedirs()
to prevent errors when a directory already exists.
1
2
3
4
5
6
| import os
# Creates all parent directories if they don’t exist. The `exist_ok=True` argument is used in functions like `os.makedirs()` to prevent errors when a directory already exists.
os.makedirs("parent/child", exist_ok=True)
# FileNotFoundError: [WinError 3] 지정된 경로를 찾을 수 없습니다
os.mkdir("parent/child")
|
os.remove()
Delete a File.
1
2
3
| import os
os.remove("file.txt")
|
os.rmdir()
Delete an Empty Directory.
1
2
3
4
5
| import os
os.rmdir("empty_folder") # Deletes the empty folder
# An error occurs if the folder is not empty.
# OSError: [WinError 145] 디렉터리가 비어 있지 않습니다: 'empty_folder'
|
os.environ.get()
Get an Environment Variable.
1
2
3
4
5
| import os
# e.g. home environment
print(os.environ.get("HOME")) # Linux/macOS, /home/jaoneol
print(os.environ.get("USERPROFILE")) # Windows, C:\Users\ycjang
|
Path Handling Module
os.path.dirname()
Returns the directory path containing target(file or directory)
(i.e., the full path of the parent directory).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| import os
# e.g. target file
remotefile = "/home/user/projects/data/file.txt"
print(f'remotefile:{remotefile}') # /home/user/projects/data/file.txt
path = os.path.dirname(remotefile)
print(f'path:{path}') # /home/user/projects/data
parts = path.split('/')
print(f'parts:{parts}') # ['', 'home', 'user', 'projects', 'data']
# e.g. target directory
remotefile = "/home/user/projects/data/mydata"
print(f'remotefile:{remotefile}') # /home/user/projects/data/mydata
path = os.path.dirname(remotefile)
print(f'path:{path}') # /home/user/projects/data
parts = path.split('/')
print(f'parts:{parts}') # ['', 'home', 'user', 'projects', 'data']
|
os.path.basename()
Returns only the filename.
1
2
3
4
5
6
7
8
9
10
11
12
| import os
# e.g. target file
remotefile = "/home/user/projects/data/file.txt"
onlyfilename = os.path.basename(remotefile) # /home/user/projects/data
print(f'onlyfilename:{onlyfilename}') # file.txt
remotefile = "/home/user/projects/data/file.tar.gz"
onlyfilename = os.path.basename(remotefile) # /home/user/projects/data
print(f'onlyfilename:{onlyfilename}') # file.tar.gz
|
os.path.join()
os.path.join()
automatically handles OS-specific path separators (/
or \
), making it cross-platform compatible.
1
2
3
4
5
6
7
8
9
10
11
| import os
# Linux
path = os.path.join("/home/user/projects", "data", "file.txt")
print(path) # /home/user/projects/data/file.txt
# Windows
path = os.path.join("/home/user/projects", "data", "file.txt")
print(path) # /home/user/projects\data\file.txt
path = path.replace("\\", "/")
print(path) # /home/user/projects\data\file.txt
|
os.path.splitext()
Split File Extension.
1
2
3
4
5
6
7
8
9
10
11
| import os
filename = "/home/user/projects/data/file.txt"
name, ext = os.path.splitext(filename)
print(name) # "/home/user/projects/data/file"
print(ext) # ".txt"
filename = "/home/user/projects/data/file.tar.gz"
name, ext = os.path.splitext(filename)
print(name) # "/home/user/projects/data/file.tar"
print(ext) # ".gz"
|
os.path.split()
Splits the path into (directory, filename).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| import os
filename = "/home/user/projects/data/file.txt"
directory, file_name = os.path.split(filename)
print(directory) # "/home/user/projects/data"
print(file_name) # "file.txt"
# If there is no trailing slash (`/`), the last element is returned as the "filename."
filename = "/home/user/projects/data/mydata"
directory, file_name = os.path.split(filename)
print(directory) # "/home/user/projects/data"
print(file_name) # "mydata"
# If there is a trailing slash (`/`), the last element is recognized as a directory, and an empty string (`""`) is returned.
filename = "/home/user/projects/data/mydata/"
directory, file_name = os.path.split(filename)
print(directory) # "/home/user/projects/data/mydata"
print(file_name) # ""
|
Str Handling Module
str.split()
1
2
3
4
5
6
7
8
9
| string = "/home/user/projects/data/file.tar.gz"
result = string.split() # Split based on spaces.
print(result) # ['/home/user/projects/data/file.tar.gz']
result = string.split(".")
print(result) # ['/home/user/projects/data/file', 'tar', 'gz']
result = string.split("/")
print(result) # ['', 'home', 'user', 'projects', 'data', 'file.tar.gz']
|
str.removeprefix() and str.removesuffix()
1
2
3
4
5
6
7
8
9
| string = "/home/user/projects/data/file.tar.gz"
result = string.removeprefix("/home")
print(result) # /user/projects/data/file.tar.gz
result = string.removeprefix("home")
print(result) # /home/user/projects/data/file.tar.gz
result = string.removesuffix(".tar.gz")
print(result) # /user/projects/data/file
|