带有示例的Python中的OS模块

Python操作系统模块

os模块为我们提供了很多功能是用于与操作系统交互。无需安装os模块,因为它是Python中的内置模块。通过使用它,我们可以执行更多任务,例如获得操作系统的名称,导航文件系统以及执行许多其他操作。

os模块中有许多指令。我们可以通过使用dir()功能。让我们来看看它,

# 导入os模块
import os 

# 打印指令
print('All directive of os module are:',dir(os))

输出结果

All directive of os module are: ['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 
'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 
'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 
'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 
'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', 
'__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', 
'_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 
'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 
'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 
'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 
'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 
'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 
'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 
'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 
'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 
'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 
'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 
'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 
'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 
'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 
'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

在本教程中,我们将介绍os模块的一些重要功能。

os模块的一些重要功能

1)操作系统名称

该函数给出了我们在自己的系统中运行的操作系统的名称。此功能的输出可能因系统而异,因为所有系统都没有使用同一操作系统。

# 导入os模块
import os 

# getting & printing the name of the OS
print( 'Name of operating system: ',os.name)

输出结果

Name of operating system:  nt #Windows nt是32位操作系统。

2) os.getcwd()

此函数返回用于执行代码的文件的当前工作指令的名称。

# 导入os模块
import os 

# getting & printing the current working directory
print( 'location of the file:',os.getcwd())

输出结果

location of the file: C:\Users\BIPIN KUMAR

3) os.chdir()

此函数用于更改用于执行代码的文件的路径。它以字符串形式的新路径作为参数。在更改目录之前,我们使用mkdir()功能

# 导入os模块
import os 

# 创建一个临时目录
os.mkdir('d:\\tempdirectory') 
# getting & printing the current working directory
print( 'location of the file:',os.getcwd())

# 更改目录
os.chdir('E:\\tempdirectory') 
# getting & printing the current working directory
print( 'New location of the file:',os.getcwd())

输出结果

location of the file: C:\Users\BIPIN KUMAR
New location of the file: E:\tempdirectory