杰瑞科技汇

Python os命令如何安全执行系统指令?

Of course! The os module in Python provides a way of using operating system-dependent functionality. It allows you to interact with the file system, manage environment variables, and execute system commands.

Python os命令如何安全执行系统指令?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide to using the os module, broken down into key areas.


Core File and Directory Operations

These are the most common tasks you'll perform with the os module.

Getting the Current Working Directory

import os
# Get the current working directory (where the script is running)
current_dir = os.getcwd()
print(f"Current working directory: {current_dir}")

Changing the Current Working Directory

import os
# Change to a new directory
# os.chdir('/path/to/your/directory')
# You can use '..' to go up one level
# os.chdir('..')
# To go back to the original directory after a change, it's good practice to save it first
original_dir = os.getcwd()
print(f"Original directory: {original_dir}")
try:
    os.chdir('/tmp')
    print(f"Changed to directory: {os.getcwd()}")
finally:
    # Use a try...finally block to ensure you always go back
    os.chdir(original_dir)
    print(f"Returned to directory: {os.getcwd()}")

Listing Directory Contents

import os
# List all files and directories in the current directory
contents = os.listdir()
print("Contents of current directory:")
print(contents)
# List contents of a specific directory
# contents = os.listdir('/home/user')

Creating and Removing Directories

import os
# Create a single directory (will raise an error if parent path doesn't exist)
# os.mkdir('new_folder')
# Create a directory tree (all necessary parents)
os.makedirs('parent_folder/child_folder', exist_ok=True)
print("Created directory tree 'parent_folder/child_folder'")
# Remove an empty directory
# os.rmdir('new_folder')
# Remove a directory tree (use with caution!)
# os.removedirs('parent_folder/child_folder')

Renaming (Moving) Files and Directories

import os
# Rename a file or directory
# os.rename('old_name.txt', 'new_name.txt')
# The 'rename' function is also used to move files/directories
# os.rename('file.txt', 'parent_folder/file.txt')

Deleting Files

import os
# Delete a file
# os.remove('unwanted_file.txt')

Getting File and Directory Information

The os.path submodule is essential for working with file paths in a platform-independent way.

Checking Path Type

import os.path
path = 'some_file.txt'
dir_path = 'some_directory'
if os.path.isfile(path):
    print(f"'{path}' is a file.")
if os.path.isdir(dir_path):
    print(f"'{dir_path}' is a directory.")
if os.path.exists(path):
    print(f"'{path}' exists.")
else:
    print(f"'{path}' does not exist.")

Getting File Metadata

import os
file_path = 'example.txt'
if os.path.exists(file_path):
    # Get file size in bytes
    size = os.path.getsize(file_path)
    print(f"Size of '{file_path}': {size} bytes")
    # Get last modification time (timestamp)
    mod_time = os.path.getmtime(file_path)
    print(f"Last modified time (timestamp): {mod_time}")
    # Convert timestamp to a readable format
    from datetime import datetime
    readable_time = datetime.fromtimestamp(mod_time)
    print(f"Last modified time (readable): {readable_time}")

Working with Paths (The Modern Way)

For modern Python (3.4+), the pathlib module is highly recommended as it provides an object-oriented interface to filesystem paths. It's more intuitive and less error-prone than manually joining strings with or os.path.join.

Python os命令如何安全执行系统指令?-图2
(图片来源网络,侵删)

However, understanding os.path.join is still very useful.

os.path.join

This function correctly joins path components using the right separator for the operating system (\ for Windows, for Linux/macOS).

import os.path
# This is safer than hardcoding separators
path = os.path.join('home', 'user', 'documents', 'report.txt')
print(path)
# Output on Linux: 'home/user/documents/report.txt'
# Output on Windows: 'home\\user\\documents\\report.txt'

Using pathlib (Recommended)

from pathlib import Path
# Create a path object
p = Path('home') / 'user' / 'documents' / 'report.txt'
print(p)
# Output: home/user/documents/report.txt (works the same on Windows)
# Check if path exists
if p.exists():
    print(f"Path '{p}' exists.")
# Get file name and extension
print(f"File name: {p.name}")         # report.txt
print(f"File stem: {p.stem}")         # report
print(f"File suffix: {p.suffix}")     # .txt
print(f"Parent directory: {p.parent}") # home/user/documents
# Create directories (equivalent to os.makedirs)
# p.mkdir(parents=True, exist_ok=True)

Executing System Commands (Use with Caution)

You can use os.system() to run a command as if you were in your terminal. This is generally not the recommended method because it's difficult to capture the output or handle errors programmatically. The subprocess module is almost always a better choice.

os.system()

This function returns the exit code of the process (0 for success, non-zero for failure).

import os
# Print the current directory (using the 'ls' or 'dir' command)
# On Linux/macOS
exit_code = os.system('ls -l')
# On Windows
# exit_code = os.system('dir')
print(f"\nCommand exited with code: {exit_code}")

Why os.system is often a bad idea:

  • No Output Capture: You can't get the result of the command (e.g., the list of files) into a Python variable.
  • Security Risk: If you build a command from user input, you can create a "shell injection" vulnerability. For example, os.system(f"cat {user_input_file}") could be exploited if user_input_file is something malicious like ; rm -rf /.

The Better Alternative: subprocess Module

For any serious work, use the subprocess module. It's designed for this purpose.

import subprocess
# Run a command and capture its output
try:
    # Run 'ls -l' and get the output as a string
    result = subprocess.run(['ls', '-l'], capture_output=True, text=True, check=True)
    print("Command executed successfully!")
    print("STDOUT:")
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print(f"Command failed with exit code {e.returncode}")
    print("STDERR:")
    print(e.stderr)
# Example on Windows
# result = subprocess.run(['dir'], capture_output=True, text=True, check=True)
# print(result.stdout)

Environment Variables

Environment variables are key-value pairs used to configure the environment for running processes.

Getting an Environment Variable

import os
# Get a specific environment variable
# Use .get() to avoid errors if the variable doesn't exist
path_var = os.environ.get('PATH')
print(f"PATH environment variable (first 100 chars): {path_var[:100]}...")
# Get the home directory
home_dir = os.environ.get('HOME') # Linux/macOS
# home_dir = os.environ.get('USERPROFILE') # Windows
print(f"Home directory: {home_dir}")

Setting an Environment Variable

import os
# Set a new environment variable for the current process
os.environ['MY_CUSTOM_VAR'] = 'hello_world'
# Verify it was set
print(f"MY_CUSTOM_VAR is now: {os.environ['MY_CUSTOM_VAR']}")
# This variable will only exist for the duration of the Python script.
# It will not affect your terminal's environment.

Summary: Which os function to use?

Task Recommended Method (os or pathlib) Notes
Joining paths os.path.join() or pathlib.Path / operator Crucial for cross-platform compatibility.
Check if file/dir exists os.path.exists() or pathlib.Path.exists() Use isfile() and isdir() for specific checks.
Get file info (size, time) os.path.getsize(), os.path.getmtime() pathlib objects also have .stat() method.
List directory contents os.listdir() or pathlib.Path.iterdir() iterdir() is more powerful.
Create a directory os.makedirs() or pathlib.Path.mkdir(parents=True) Use exist_ok=True to prevent errors if it already exists.
Execute a command Avoid os.system(). Use subprocess module instead. subprocess is safer, more flexible, and allows for I/O capture.
Get environment variables os.environ.get() Use .get() to handle missing variables gracefully.
分享:
扫描分享到社交APP
上一篇
下一篇