OS模块是Python的内置模块,提供了与操作系统交互的功能。下面是一些常见的OS模块的使用示例:
- 获取当前工作目录:
import os
current_dir = os.getcwd()
print(current_dir)
- 创建目录:
import os
os.mkdir("new_directory")
- 删除目录:
import os
os.rmdir("directory_to_delete")
- 列出目录中的文件和子目录:
import os
contents = os.listdir("directory_path")
for item in contents:
print(item)
- 检查文件或目录是否存在:
import os
exists = os.path.exists("file_or_directory_path")
if exists:
print("File or directory exists!")
else:
print("File or directory does not exist!")
- 运行操作系统命令:
import os
os.system("command_to_execute")
这只是OS模块的一小部分功能示例,还有其他功能,如文件操作、路径操作、环境变量等。你可以查阅Python的官方文档或其他教程来获取更多使用方法。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/855668.html