OS模块的常用内置方法

简介: chdir修改当前工作目录到指定目录Change the current working directory to the specified path.chmod修改一个文件的访问权限Change the access permissions of a file.

chdir

修改当前工作目录到指定目录
Change the current working directory to the specified path.

chmod

修改一个文件的访问权限
Change the access permissions of a file.

chown

把一个目录的属主和属组修改成另一个数字的属主和属组
Change the owner and group id of path to the numeric uid and gid

chroot

修改根目录(在liunx下使用)
Change root directory to path.

ctermid

返回一个进程的控制台的名字
Return the name of the controlling terminal for this process.

getcwd

以字符串格式显示当前工作目录(python解释器的工作目录)
Return a unicode string representing the current working directory.

getegid

获取当前进程的有效组ID
Return the current process's effective group id.

getenv

获取系统的环境变量,环境变量不存在时返回None
Get an environment variable, return None if it doesn't exist.

geteuid

获取当前进程的有效用户ID
Return the current process's effective user id.

getgid

获取当彰进程的组ID
Return the current process's group id.

getgrouplist

返回指定用户的基本组的列表
getgrouplist(user, group) -> list of groups to which a user belongs
Returns a list of groups to which a user belongs.

getgroups

获取一个进程的附加组ID的列表
Return list of supplemental group IDs for the process.

getloadavg

返回当前一个由系统1分钟,5分钟和15分钟的平均负载使用率组成的元组
Return average recent system load information.
Return the number of processes in the system run queue averaged over
the last 1, 5, and 15 minutes as a tuple of three floats.
Raises OSError if the load average was unobtainable.

getlogin

返回当前系统的实际登陆的用户名
Return the actual login name.

getpid

返回当前的进程ID号
Return the current process id.

getppid

返回当前进程的父进程的ID号
Return the parent's process id.

getuid

返回当前进程的用户ID
Return the current process's user id.

kill

结束一个进程
Kill a process with a signal.

killpg

结束掉一个进程组
Kill a process group with a signal.

listdir

返回指定路径下由所有所有文件的名字组成的列表,未指定路径则默认为当前路径
Return a list containing the names of the files in the directory.
path can be specified as either str or bytes.  If path is bytes,
  the filenames returned will also be bytes; in all other circumstances
  the filenames returned will be str.
If path is None, uses the path='.'.

lseek

设置并返回指定文件的指针的位置,
Set the position of a file descriptor.  Return the new position.
Return the new cursor position in number of bytes
relative to the beginning of the file.

major

获取一个设备文件的主设备号
Extracts a device major number from a raw device number.

makedev

在系统上创建一个新的设备文件
Composes a raw device number from the major and minor device numbers.

makedirs

递归创建多层目录,如果目标目录已经存在,则报错
Super-mkdir; create a leaf directory and all intermediate ones.  Works like
mkdir, except that any intermediate path segment (not just the rightmost)
will be created if it does not exist. If the target directory already
exists, raise an OSError if exist_ok is False. Otherwise no exception is
raised.  This is recursive.

minor

获取一个设备文件的次设备号
Extracts a device minor number from a raw device number.

mkdir

创建一个目录
Create a directory.

mkfifo

创建一个管道文件
If dir_fd is not None, it should be a file descriptor open to a directory,
  and path should be relative; path will then be relative to that directory.
dir_fd may not be implemented on your platform.
  If it is unavailable, using it will raise a NotImplementedError.

nice

为一个进程增加属性,也即修改一个进程的nice值
Add increment to the priority of process and return the new priority.

open

在硬件层面打开文件,返回一个整型的文件描述符
Open a file for low level IO.  Returns a file descriptor (integer).

putenv

修改或增加一个环境变量
Change or add an environment variable.

read

读取指定文件的文件描述符,返回字节对象
Read from a file descriptor.  Returns a bytes object.

remove

删除指定文件
Remove a file (same as unlink()).

removedirs

删除多层目录,
Super-rmdir; remove a leaf directory and all empty intermediate
ones.  Works like rmdir except that, if the leaf directory is
successfully removed, directories corresponding to rightmost path
segments will be pruned away until either the whole path is
consumed or an error occurs.  Errors during this latter phase are
ignored -- they generally mean that a directory was not empty.

rename

为一个文件或目录重命名
Rename a file or directory.

rmdir

删除一个目录
Remove a directory.

setegid

设置当前进程的有效组ID
Set the current process's effective group id.

seteuid

设置当前进程的有效用户ID
Set the current process's effective user id.

setgid

设置当前进程的组ID
Set the current process's group id.

setuid

为当前进程设置用户ID
Set the current process's user id.

stat

显示指定路径的详细信息
Perform a stat system call on the given path.

strerror

把错误号码转换成信息字符串
Translate an error code to a message string.

symlink

为指定目录创建软链接
symlink(src, dst, target_is_directory=False, *, dir_fd=None)
Create a symbolic link pointing to src named dst.

sync

强制写入内容到磁盘中
Force write of everything to disk.

system

在子shell中执行系统命令
Execute the command in a subshell.

times

返回系统时间信息的集合
Return a collection containing process timing information.

umask

为文件或目录设置并返回新权限模式
Set the current numeric umask and return the previous umask.

uname

返回当前操作系统的内核信息
Return an object identifying the current operating system.

unlink

删除一个软链接
unlink(path, *, dir_fd=None)
Remove a file (same as remove()).

unsetenv

删除一个环境变量
Delete an environment variable.

utime

为指定路径设置最后访问时间和最后修改时间
utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)
Set the access and modified time of path.

walk

给定目录名,返回一个由当前目录名,当前目录的子目录以及当前目录下的普通文件组成的元组
Directory tree generator.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple
    dirpath, dirnames, filenames
dirpath is a string, the path to the directory.  dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name).
目录
相关文章
|
5月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
122 17
|
6月前
|
Python
Python教程:os 与 sys 模块详细用法
os 模块用于与操作系统交互,主要涉及夹操作、路径操作和其他操作。例如,`os.rename()` 重命名文件,`os.mkdir()` 创建文件夹,`os.path.abspath()` 获取文件绝对路径等。sys 模块则用于与 Python 解释器交互,常用功能如 `sys.path` 查看模块搜索路径,`sys.platform` 检测操作系统等。这些模块提供了丰富的工具,便于开发中处理系统和文件相关任务。
262 14
|
7月前
|
监控 关系型数据库 MySQL
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
417 30
|
9月前
|
缓存 安全 Linux
Linux系统查看操作系统版本信息、CPU信息、模块信息
在Linux系统中,常用命令可帮助用户查看操作系统版本、CPU信息和模块信息
1649 23
|
11月前
|
Python
文件元数据获取方法对比:`os.path` 与 `os.stat`
本文对比了Python中两种获取文件元数据的方法:`os.path`和`os.stat`。通过示例代码展示了如何获取文件大小和修改时间,并从性能、功能性和代码可读性三方面进行了详细对比。最终给出了根据具体需求选择合适方法的最佳实践建议。
176 2
|
12月前
|
Python
Python实用记录(四):os模块-去后缀或者改后缀/指定目录下图片或者子目录图片写入txt/csv
本文介绍了如何使用Python的os模块来操作文件,包括更改文件后缀、分割文件路径和后缀、将指定目录下的所有图片写入txt文档,以及将指定目录下所有子目录中的图片写入csv文档,并为每个子目录分配一个标签。
174 1
|
11月前
|
JavaScript 前端开发 Python
python中的OS模块的基本使用
欢迎来到瑞雨溪的博客,一名热爱JavaScript与Vue的大一学生。博客分享前端技术及全栈开发经验,持续更新中,期待您的关注和支持!🎉🎉🎉
164 0
|
安全 测试技术 数据库
Python编程--sys模块及OS模块简单用例
Python编程--sys模块及OS模块简单用例
130 1

推荐镜像

更多