python的os包总结

Python 的 os 模块提供了一系列与操作系统交互的功能,使你可以使用Python代码来执行许多与操作系统相关的任务,比如文件和目录操作、环境变量处理、进程管理等。以下是对 os 模块的一些详细介绍和常见用法。

导入 os 模块

import os

文件和目录操作

获取当前工作目录
current_directory = os.getcwd()
print(current_directory)
改变当前工作目录
os.chdir('/path/to/new/directory')
列出目录内容
directory_contents = os.listdir('/path/to/directory')
print(directory_contents)
创建目录
os.mkdir('new_directory')
创建多级目录
os.makedirs('parent_directory/child_directory')
删除文件
os.remove('file_to_delete.txt')
删除目录
os.rmdir('directory_to_delete')
删除多级目录
os.removedirs('parent_directory/child_directory')
重命名文件或目录
os.rename('old_name.txt', 'new_name.txt')
获取文件或目录的状态
file_stat = os.stat('file_or_directory')
print(file_stat)

环境变量

获取环境变量
path = os.getenv('PATH')
print(path)
设置环境变量
os.environ['NEW_VAR'] = 'value'
删除环境变量
os.environ.pop('NEW_VAR', None)

路径操作

os.path 模块提供了对路径名的常用操作。

获取文件的绝对路径
absolute_path = os.path.abspath('file.txt')
print(absolute_path)
获取文件的目录名
dir_name = os.path.dirname('/path/to/file.txt')
print(dir_name)
获取文件的基名
base_name = os.path.basename('/path/to/file.txt')
print(base_name)
判断路径是否存在
path_exists = os.path.exists('/path/to/file_or_directory')
print(path_exists)
判断是否为目录
is_directory = os.path.isdir('/path/to/directory')
print(is_directory)
判断是否为文件
is_file = os.path.isfile('/path/to/file')
print(is_file)
拼接路径
full_path = os.path.join('/path/to/directory', 'file.txt')
print(full_path)

进程管理

执行系统命令
os.system('ls -l')
创建子进程
pid = os.fork()
if pid == 0:
    # 子进程
    print('This is the child process')
else:
    # 父进程
    print(f'This is the parent process, child process ID: {pid}')
获取当前进程ID
current_pid = os.getpid()
print(current_pid)
获取父进程ID
parent_pid = os.getppid()
print(parent_pid)

文件描述符操作

打开文件
fd = os.open('file.txt', os.O_RDWR | os.O_CREAT)
读取文件
num_bytes = os.read(fd, 100)
写入文件
os.write(fd, b'This is a test')
关闭文件
os.close(fd)

其他有用功能

获取用户的主目录
home_directory = os.path.expanduser('~')
print(home_directory)
获取临时文件目录
temp_directory = os.path.gettempdir()
print(temp_directory)

示例:文件操作脚本

以下是一个完整的脚本示例,展示了如何使用 os 模块来进行基本的文件和目录操作:

import os

# 获取当前工作目录
current_directory = os.getcwd()
print(f'Current directory: {current_directory}')

# 创建新目录
new_directory = 'new_folder'
os.mkdir(new_directory)
print(f'Created new directory: {new_directory}')

# 改变工作目录
os.chdir(new_directory)
print(f'Changed to new directory: {os.getcwd()}')

# 创建新文件并写入内容
file_name = 'example.txt'
with open(file_name, 'w') as file:
    file.write('Hello, world!')

print(f'Created and wrote to file: {file_name}')

# 读取文件内容
with open(file_name, 'r') as file:
    content = file.read()

print(f'Content of {file_name}: {content}')

# 删除文件
os.remove(file_name)
print(f'Deleted file: {file_name}')

# 回到原来的工作目录
os.chdir(current_directory)
print(f'Returned to original directory: {current_directory}')

# 删除新创建的目录
os.rmdir(new_directory)
print(f'Deleted directory: {new_directory}')

总结

Python 的 os 模块是一个强大的工具,提供了与操作系统交互的各种功能。从文件和目录操作到环境变量管理,再到进程控制和路径处理,os 模块使得Python脚本能够轻松完成许多系统级任务。如果你需要进行系统级编程或编写与操作系统交互的脚本,os 模块将是一个非常有用的工具。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值