python的系统模块_Python操作系统模块

python的系统模块

Python OS module provides easy functions that allow us to interact and get Operating System information and even control processes up to a limit.

Python OS模块提供了简单的功能,使我们可以进行交互并获取操作系统信息,甚至可以控制进程。

Python OS模块 (Python OS Module)

The functions OS module provides allows us to operate on underlying Operating System tasks, irrespective of it being a Windows Platform, Macintosh or Linux. In this lesson, we will review these functions and what we can do with these.

OS模块提供的功能使我们可以执行基本的操作系统任务,而无论它是Windows平台,Macintosh还是Linux。 在本课程中,我们将回顾这些功能以及我们可以做什么。

Let us start our journey with these functions and what information they offer.

让我们从这些功能及其提供的信息开始我们的旅程。

Python导入操作系统 (Python import os)

Please note that first of all we have to import OS module in our program, then only we can execute any of it’s functions.

请注意,首先我们必须在程序中导入 OS模块,然后才可以执行其任何功能。

操作系统名称 (os.name)

This function gives the name of the OS module it imports. This differs based on the underlying Operating System. Currently, it registers ‘posix’, ‘os2’, ‘ce’, ‘nt’, ‘riscos’ and ‘java’.

该函数给出了它导入的OS模块的名称。 这取决于基础操作系统。 当前,它注册“ posix”,“ os2”,“ ce”,“ nt”,“ riscos”和“ java”。

Let’s execute this on the system:

让我们在系统上执行此操作:

>>> print(os.name)
posix

Clearly, this can output different platforms based on the interpreter.

显然,这可以基于解释器输出不同的平台。

os.viron (os.environ)

environ is not a function but a process parameter through which we can access environment variables of the system.
Let’s see sample code snippet:

environ不是函数,而是过程参数,通过它我们可以访问系统的环境变量。
让我们看一下示例代码片段:

import os
output = os.environ['HOME']
print(output)

When we run this script, the following will be the output:

python os environment variable

当我们运行此脚本时,将输出以下内容:

We can use it to work with the environment variables, read more at Python set environment variable.

我们可以使用它来处理环境变量,更多内容请参见Python set environment variable

os.execvp (os.execvp)

execvp function is one of the ways to run other commands on the system.
Let’s see sample code snippet for this function:

execvp函数是在系统上运行其他命令的方法之一。
让我们看看此功能的示例代码片段:

import os
program = "python"
arguments = ["hello.py"]
print(os.execvp(program, (program,) +  tuple(arguments)))

For this, we just created a sample script as hello.py with following code:

为此,我们仅使用以下代码创建了一个示例脚本hello.py

print('Hello')

When we run this script, the following will be the output:

python os module, python os execute other script

当我们运行此脚本时,将输出以下内容:

os.getuid (os.getuid)

This os module function returns current process’s user ID or UID, as it is populary known.

众所周知,此os模块函数返回当前进程的用户ID或UID。

>>> os.getuid()
501

So, the current shell process ID is 501.

因此,当前的shell进程ID为501。

os.rename (os.rename)

With the python os rename function, we can easily rename a file.

使用python os重命名功能,我们可以轻松地重命名文件。

import os
fileDir = "JournalDev.txt"
os.rename(fd,'JournalDev_Hi.txt')

Note that for this, we must provide correct permissions to our script.

请注意,为此,我们必须为脚本提供正确的权限。

操作系统 (os.system)

Python os system function allows us to run a command in the Python script, just like if I was running it in my shell. For example:

Python os系统功能允许我们在Python脚本中运行命令,就像在外壳中运行该命令一样。 例如:

import os
currentFiles = os.system("users > users.txt")

When I ran this script, a new file was made in the same directory with name users.txt and content String as ‘shubham’ as this is returned by original shell as well:

Python OS System command

当我运行此脚本时,在同一目录中创建了一个新文件,名称为users.txt,内容字符串为'shubham',这也是由原始shell返回的:

Note that this is a very powerful command and should be used cautiously.

请注意,这是一个非常强大的命令,应谨慎使用。

错误 (os.error)

Python os module error class is the base class for I/O related errors. So we can catch IO errors using OSError in the except clause.

Python os模块错误类是I / O相关错误的基类。 因此,我们可以使用except子句中的OSError捕获IO错误。

import os

try:
    f = open('abc.txt', 'r')  # file is missing
except OSError:
    print('Error')

os.getpid (os.getpid)

This function returns current process ID or PID, as it is populary known.

众所周知,此函数返回当前进程ID或PID。

>>> os.getpid()
71622

So, the current shell process’s user ID is 71622.

因此,当前Shell进程的用户ID为71622。

os.listdir (os.listdir)

This function just lists out the files and directories present in the current working directory.

此功能仅列出当前工作目录中存在的文件和目录。

>>> import os
>>> os.listdir()
['.DS_Store', '.localized', 'JournalDev', 'Java', 'Python']

It returns an iterable list of directory and file names.

它返回目录和文件名的可迭代列表。

os.uname (os.uname)

This function return information which identifies current Operating System on which this is executed.

此函数返回标识执行此操作的当前操作系统的信息。

>>> os.uname()
posix.uname_result(sysname='Darwin', nodename='Shubham.local', release='17.2.0', version='Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/RELEASE_X86_64', machine='x86_64')

That was prettry detailed actually.

这实际上是很详细的。

导入os.path vs导入os (import os.path vs import os)

os.path works strangely actually. It looks like os packaged with a submodule path but actually, os is a normal module which operate with sys.module to support os.path. Let us list what happens behind the scenes:

os.path实际上工作异常。 看起来os带有一个子模块path但实际上os是一个普通的模块,与sys.module一起运行以支持os.path 。 让我们列出幕后发生的事情:

  • When Python starts, it loads many modules into sys.module.

    当Python启动时,它将许多模块加载到sys.module
  • os module is also loaded when Python starts. It assigns its path to the os specific module attribute.

    Python启动时,也会加载os模块。 它将其path分配给os特定的模块属性。
  • It injects sys.modules['os.path'] = path so that you’re able to do import os.path as though it was a submodule.

    它注入sys.modules['os.path'] = path以便您可以像import os.path一个子模块一样import os.path

摘要 (Summary)

In this lesson, we read about various functions provided by OS module in Python and saw how they work. See more lessons on Python here.

在本课程中,我们阅读了Python中OS模块提供的各种功能,并了解了它们如何工作。 在此处查看有关Python的更多课程。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/17232/python-os-module

python的系统模块

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值