python 示例_带有示例的Python中的OS模块

python 示例

Python操作系统模块 (Python os Module)

The os module provides us with a lot of functions that used to interact with the operating system. There is no need to install the os module because it is an inbuilt module in Python. By using it, we can perform more tasks such as we can get the name of the operating system, navigate the file system, and many other operations are performed.

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

There are many directives in the os module. We can know the all directive by using the dir() function. Let's see it,

os模块中有许多指令。 通过使用dir()函数,我们可以知道all指令。 让我们来看看它,

# importing the os module
import os 

# printing the directives
print('All directive of os module are:',dir(os))

Output

输出量

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']

In this tutorial, we will cover some important functions of the os module.

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

os模块的一些重要功能 (Some important function of the os module)

1) os.name

1)操作系统名称

This function gives the name of the operating system that we are running in own system. The output of this function may vary from system to system because all are not using the same operating system.

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

# importing the os module
import os 

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

Output

输出量

Name of operating system:  nt #windows nt is 32 bit operating system.

2) os.getcwd()

2)os.getcwd()

This function returns the name of the current working directive of the file that was used to execute the code.

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

# importing the os module
import os 

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

Output

输出量

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

3) os.chdir()

3)os.chdir()

This function is used to change the path of the file that was used to execute the code. It takes the new path in the form of string as a parameter. Before going to change the directory, we make a new directory by using the mkdir() function.

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

# importing the os module
import os 

# creates a temporary directory
os.mkdir('d:\\tempdirectory') 
# getting & printing the current working directory
print( 'location of the file:',os.getcwd())

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

Output

输出量

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


翻译自: https://www.includehelp.com/python/os-module-in-python-with-examples.aspx

python 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值