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:

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

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:

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

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:

当我运行此脚本时,在同一目录中创建了一个新文件,名称为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 ostry: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的系统模块

python的系统模块_Python操作系统模块相关推荐

  1. [转载] python的系统模块_Python操作系统模块

    参考链接: 带有示例的Python中的OS模块 python的系统模块 Python OS module provides easy functions that allow us to intera ...

  2. python 苹果系统 交互_Python常用模块os——与操作系统交互

    os.getcwd() 返回当前工作目录绝对路径 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit ...

  3. python 苹果系统 交互_Python常用模块os--与操作系统交互

    os.getcwd() 返回当前工作目录绝对路径 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit ...

  4. python 多层包多模块_python Modules模块操作

    今天学习python的Modules模块操作,并记录学习过程欢迎大家一起交流分享. 首先新建一个python文件命名为my_module.py的自定义moudle文件,在这个文件中进行模块代码编写: ...

  5. python如何自定义模块_python自定义模块和开源模块使用方法

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  6. python 自定义模块_Python 自定义模块路径

    问题: 假设我们自己写了一个名为 log_config.py 的日志记录程序模块.此模块会被其他程序所引用,如下例子. import log_config logger = log_config.lo ...

  7. python shelve模块_python常用模块之shelve模块

    python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...

  8. python常用运维模块_python常用模块之一

    sys模块: sys模块是提供关于python本身的详细内在的信息的模块. sys.executable变量,它包含python解释器的路径 sys.platform变量,告诉我们现在处于什么操作系统 ...

  9. python pp模块_python常用模块

    1.re模块 re模块用于对python的正则表达式的操作 1.1 什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物 ...

最新文章

  1. Docker 镜像优化:从 1.16GB 到 22.4MB
  2. 暨南大学计算机专业录取分数线2019,暨南大学2019年在广东省各专业录取分数线...
  3. ZOJ Monthly, January 2013
  4. 常考数据结构与算法:最长公共子串
  5. tcp/ip 协议栈Linux内核源码分析12 udp套接字发送流程一
  6. (转载)VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)
  7. SQL 新加字段查询窗口报错
  8. nodejs定时任务node-schedule
  9. Python数据结构和算法
  10. 基于 abp vNext 和 .NET Core 开发博客项目
  11. 豆瓣9.6分!再一次被BBC的纪录片震惊!
  12. CodeForces 771C Bear and Tree Jumps 树形DP
  13. OMM机房监控系统引领机房监控新趋势
  14. 云小课|VMware备份上云学习专列来了,快加入吧~
  15. SpringBoot之Bean之条件注入@ConditionalOnProperty
  16. 07树莓派下的浏览器
  17. iOS 使用FFmpeg
  18. Luogu1541[NOIp2010 TG] 乌龟棋
  19. 软件工程复习笔记 用例图
  20. mouseenter鼠标事件

热门文章

  1. discuz开发学习
  2. 技术专题之-技术概述的目录
  3. 为什么我比别人差这么多?
  4. [转载] Python字符串的截取
  5. [转载] pandas DataFrame apply()函数(1)
  6. ARM、DSP、FPGA的区别
  7. Spring的数据库编程浅入浅出——不吹牛逼不装逼
  8. Codeforces 466E Information Graph
  9. macOs 使用Homebrew升级到MySQL 8系列之后,php无法连接解决方法
  10. BerkeleyDB环境API