The os module in Python provides a way to interact with the operating system, allowing you to perform various tasks such as file and directory operations, environment variables manipulation, and executing system commands. Here's a brief overview of some commonly used functionalities provided by the os module:
Working with Files and Directories:
- os.getcwd(): Get the current working directory.
- os.chdir(path): Change the current working directory to the specified path.
- os.listdir(path='.'): List files and directories in the specified directory.
- os.mkdir(path): Create a new directory.
- os.makedirs(path): Create directories recursively.
- os.remove(path): Remove a file.
- os.rmdir(path): Remove an empty directory.
- os.removedirs(path): Remove directories recursively.
- os.rename(src, dst): Rename a file or directory.
- os.path.exists(path): Check if a path exists.
Environment Variables:
- os.environ: A dictionary containing the environment variables.
- os.getenv(var_name, default=None): Get the value of the specified environment variable.
- os.putenv(var_name, value): Set the value of the specified environment variable.
- os.unsetenv(var_name): Unset (delete) the specified environment variable.
Miscellaneous:
- os.system(command): Execute the command in the system shell.
- os.path.join(path1, path2, ...): Join one or more path components intelligently.
- os.path.abspath(path): Return the absolute version of a path.
- os.path.basename(path): Return the base name of a path.
- os.path.dirname(path): Return the directory name of a path.
- os.path.isfile(path): Check if a path is a regular file.
- os.path.isdir(path): Check if a path is a directory.
Here's a simple example demonstrating the use of os module functions to work with files and directories:
Here are basic examples for each of the functionalities you listed:
import os
cwd = os.getcwd()
print("Current working directory:", cwd)
files = os.listdir(cwd)
print("Files and directories in current directory:", files)
new_dir = os.path.join(cwd, 'new_directory')
os.mkdir(new_dir)
path = os.path.join(cwd, 'file.txt')
if os.path.exists(path):
print("Path exists:", path)
else:
print("Path does not exist:", path)
import os
cwd = os.getcwd()
print("Current working directory:", cwd)
os.chdir('/path/to/new/directory')
print("New working directory:", os.getcwd())
files = os.listdir()
print("Files and directories:", files)
os.mkdir('new_directory')
os.makedirs('parent_dir/sub_dir')
os.remove('file.txt')
os.rmdir('empty_directory')
os.removedirs('parent_dir/sub_dir')
os.rename('old_name.txt', 'new_name.txt')
if os.path.exists('file.txt'):
print("Path exists")
else:
print("Path does not exist")
home_dir = os.getenv('HOME')
print("Home directory:", home_dir)
os.putenv('MY_VAR', 'value')
os.unsetenv('MY_VAR')
os.system('ls -l')
path = os.path.join('/path/to', 'directory', 'file.txt')
print("Joined path:", path)
abs_path = os.path.abspath('file.txt')
print("Absolute path:", abs_path)
basename = os.path.basename('/path/to/file.txt')
print("Base name:", basename)
dirname = os.path.dirname('/path/to/file.txt')
print("Directory name:", dirname)
print("Is file?", os.path.isfile('/path/to/file.txt'))
print("Is directory?", os.path.isdir('/path/to/directory'))