Python编程语言之os

Python编程语言之os

os —— Miscellaneous operating system interfaces

  • os的源码位置:os源码
  • 可以从源码出发进行os的分析。

os分析

官方文档说明

  • Python的API文档给出的信息说明是:

    This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.
    Notes on the availability of these functions:

    • The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function os.stat(path) returns stat information about path in the same format (which happens to have originated with the POSIX interface).
    • Extensions peculiar to a particular operating system are also available through the os module, but using them is of course a threat to portability.
    • All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned.
    • An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.
    • If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.
  • 还有给出的注意提示:

    Note All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.

文档分析:

  • 首先os是Python编程语言中的一个模块(module),这个模块提供了一种使用操作系统的便携式方式
  • 如果您只是想读取(read)或者写入(write)文件,可以使用Python编程语言的内置函数open(),如果要获取操作时的路径,就需要去参阅os.path模块了,如果要读取命令行中所有文件中的所有行,请参阅fileinput模块了。有关创建临时文件和临时文件目录的信息,请参阅tempfile模块,有关高级文件和高级文件目录的处理,请参阅shutil模块了。
  • 关于这些功能的可用性的说明:
    • 所有内置操作系统相关的Python模块的设计都是这样的,只要相同的功能可用,它就使用相同的接口;例如,函数os.stat(path)以相同的格式(恰好源自POSIX接口)返回有关路径的统计信息。
    • 特定操作系统特有的扩展也可以通过os模块获得,但使用它们当然是可移植性的威胁。
    • 接受路径或者文件名的所有函数都接受字节和字符串对象,如果返回路径或者文件名,则会生成相同类型的对象。
    • 一个”Availability: Unix”的提示表示此函数通常在Unix系统上找到。它没有声明它在特定操作系统上的存在。
    • 如果没有进行特别说明,Mac OS X支持声称“Availability:Unix”的所有功能,它构建在Unix的核心之上。
  • os语义就是操作系统(Operate System),所以在Python编程语言中的os模块的功能也是和操作系统关联起来的,os模块可以处理文件和目录(这些处理内容需要手动进行处理的操作),就比如说:显示当前路径下所有文件/删除某个文件/获取文件大小等等。
  • 另外一个是os模块不受任何操作系统平台的影响,比如在linux下使用os.path.abspath(__file__)(返回当前路径下的绝对路径。在大多数平台上,这相当于调用函数normpath(),如下所示:normpath(join(os.getcwd(), path))) ,在Windows下也同样可以使用os,path.abspath(__file__)收获相同的效果(获取当前的绝对路径)。

注意:如果文件名和路径无效或无法访问,或者其他参数类型正确但操作系统不接受,则此模块中的所有函数都会引发“OSError”。


os模块使用过的方法

os.name

The name of the operating system dependent module imported. The following names have currently been registered: posix, nt, os2, ce, java, riscos.
显示当前使用的平台

os.path.abspath(path)

这个在上面的已经分析过了,获取当前路径的绝对路径显示出来。

os.path.dirname(path)

Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().
返回该路径的父目录

os.path.join(path, *paths)

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
连接目录与文件名或者目录,结果为path/name

  • 测试用例
>>> os.name # 没有引入os模块,报出错误
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.name
'nt'    # Windows
>>> os.name
'posix'     # Linux
>>> os.path.dirname('test')
''      # 当初路径下的父目录
>>> os.path.dirname('C:\\projects\\workspace')
'C:\\projects'      # 当初路径下的父目录
>>> os.path.abspath('test')
'C:\\Users\\JackDan9\\test'     # 显示test路径的绝对路径
>>> os.path.dirname(os.path.abspath('test'))
'C:\\Users\\JackDan9'   # 显示test路径的绝对路径的父目录
>>> os.path.join('/home/jackdan','test')
'/home/jackdan/test'
>>>

JackDan Thinking

猜你喜欢

转载自blog.csdn.net/XXJ19950917/article/details/81355974