OS模块是Python的内置模块,提供了与操作系统交互的功能。下面是一些常见的OS模块的使用示例
OS模块是Python的内置模块,提供了与操作系统交互的功能。下面是一些常见的OS模块的使用示例:
- 获取当前工作目录:
importos
current_dir=os.getcwd()
print(current_dir)
importos
os.mkdir("new_directory")
importos
os.rmdir("directory_to_delete")
importos
contents=os.listdir("directory_path")
foritemincontents:
print(item)
importos
exists=os.path.exists("file_or_directory_path")
ifexists:
print("Fileordirectoryexists!")
else:
print("Fileordirectorydoesnotexist!")
importos
os.system("command_to_execute")
这只是OS模块的一小部分功能示例,还有其他功能,如文件操作、路径操作、环境变量等。你可以查阅Python的官方文档或其他教程来获取更多使用方法。
版权声明
本文仅代表作者观点,不代表博信信息网立场。