array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 Python中的OS module如何使用 - 爱码网

这篇“Python中的OS module如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python中的OS module如何使用”文章吧。

Python的os模块是用于与操作系统进行交互的标准库之一,它提供了许多有用的函数和变量,以便处理文件和目录。下面是一些常见的os模块函数的用法:

1.获取当前工作目录:

import os
cwd = os.getcwd()
print(cwd)

2.切换当前工作目录:

import os
os.chdir('/path/to/new/directory')

3.列出目录中的所有文件和子目录:

import os
files = os.listdir('/path/to/directory')
print(files)

4.检查给定的路径是否为目录:

import os
path = '/path/to/directory'
if os.path.isdir(path):
    print("It's a directory")
else:
    print("It's not a directory")

5.检查给定的路径是否为文件:

import os
path = '/path/to/file'
if os.path.isfile(path):
    print("It's a file")
else:
    print("It's not a file")

6.获取文件的大小(以字节为单位):

import os
path = '/path/to/file'
size = os.path.getsize(path)
print(size)

7.检查给定的路径是否存在:

import os
path = '/path/to/file_or_directory'
if os.path.exists(path):
    print("It exists")
else:
    print("It doesn't exist")

8.创建新目录:

import os
path = '/path/to/new/directory'
os.mkdir(path)

9.递归创建新目录(如果目录不存在):

import os
path = '/path/to/new/directory'
os.makedirs(path, exist_ok=True)

10.删除文件或空目录:

import os
path = '/path/to/file_or_directory'
os.remove(path)

11.递归删除目录及其内容:

import os
path = '/path/to/directory'
os.system('rm -rf ' + path)

其它一些便捷用法:

12.os.path.splitext() 方法是将一个路径分割成文件名和扩展名两部分。它会以文件名中的最后一个 "." 作为分隔符,将文件名和扩展名分开。例如,如果文件路径为 "/path/to/file.txt",则 os.path.splitext() 方法会返回一个元组 ("/path/to/file", ".txt")。

需要注意的是,如果文件名中没有 ".",则返回的扩展名为空字符串。如果文件名以 "." 开头,则视为无扩展名文件,os.path.splitext() 方法将返回 (文件路径, '')。

以下是一个例子:

import os
path = '/path/to/file.txt'
file_name, ext = os.path.splitext(path)
print('文件名为:', file_name)
print('扩展名为:', ext)

输出结果为:

文件名为: /path/to/file
扩展名为: .txt

13.设置文件权限:

import os
os.chmod('/path/to/file', 0o777) # 设置读、写、执行权限

os.chmod() 方法可以用来修改文件或目录的访问权限。它接受两个参数:文件路径和新的权限模式。权限模式可以使用八进制数表示,每个位表示不同的权限。

以下是一些权限模式的示例:

  • 0o400:只读权限

  • 0o200:写入权限

  • 0o100:执行权限

  • 0o700:所有权限

14.获取CPU数量:

import os
cpu_count = os.cpu_count()
print('CPU数量为:', cpu_count)

需要注意的是,os.cpu_count() 返回的CPU数量是物理CPU核心数,并不包括超线程技术的虚拟核心。在具有超线程技术的系统中,每个物理CPU核心都会被划分为两个虚拟核心,因此os.cpu_count()方法可能会返回比实际CPU核心数多的数量。

另外,os.cpu_count()方法在不同的操作系统上可能会有不同的实现。在某些操作系统上,它可能只返回逻辑CPU核心数,而不是物理CPU核心数。因此,在使用该方法时,最好查阅相关的文档以获取更多信息。

15.启动新进程:

import os
os.system('notepad.exe')

os.system() 方法可以在操作系统上执行一个命令,并返回命令的退出状态码。它的参数是一个字符串类型的命令,可以是任何有效的系统命令。

以下是一个示例,演示了如何使用os.system()方法来执行一个简单的命令:

import os
os.system('echo "Hello, World!"')

上述代码将输出Hello, World!字符串,并返回命令的退出状态码(通常为0表示成功)。

需要注意的是,os.system() 方法会阻塞当前进程,直到命令执行完成。如果希望在执行命令时不阻塞当前进程,可以考虑使用subprocess模块中的其他方法,如subprocess.Popen()。

以下是另一个示例,演示了如何使用os.system()方法来执行一个复杂的命令,比如在Linux系统上使用wget下载一个文件:

import os
url = 'https://example.com/file.zip'
output_dir = '/path/to/output'
command = f'wget {url} -P {output_dir}'
os.system(command)

上述代码会将url参数指定的文件下载到output_dir参数指定的目录中,并返回命令的退出状态码。

16.os.environ:这是一个包含当前环境变量的字典。可以使用os.environ[key]获取特定环境变量的值。

17.os.exec*():这些方法允许Python程序在当前进程中执行其他程序,取代当前进程。例如,os.execv() 方法可以使用指定的参数列表执行一个程序,取代当前进程。

18.os.fork():这个方法可以在Unix或Linux操作系统上创建一个子进程,用于并行执行程序。子进程将复制父进程的所有内存内容,包括代码、数据和堆栈等,因此可以在父进程的基础上继续执行程序。

19.os.kill():这个方法用于向指定进程发送信号。可以使用os.kill(pid, signal)方法向指定的进程发送指定的信号。常用的信号包括SIGINT(中断信号)、SIGTERM(终止信号)和SIGKILL(强制终止信号)等。

20.os.pipe():这个方法可以创建一个管道,用于在进程之间进行通信。os.pipe()方法将返回两个文件描述符,一个用于读取管道数据,另一个用于写入管道数据。

21.os.wait():这个方法可以等待子进程的结束,然后返回子进程的状态码。可以使用os.waitpid(pid, options)方法等待指定的进程结束,并返回进程的状态码。

22.os模块可以用来操作文件路径。例如,os.path.join(path, *paths)可以将多个路径拼接成一个完整路径,os.path.abspath(path)可以将相对路径转换为绝对路径,os.path.split(path)可以将路径分割成目录和文件名。

23.遍历目录树

import os
def list_files(path):
    for root, dirs, files inos.walk(path):
        for file in files:
            print(os.path.join(root, file))
list_files('.')

这段代码可以遍历当前工作目录及其子目录下的所有文件,并打印出它们的完整路径。

os.walk()是os模块中一个非常有用的函数,用于遍历指定目录及其子目录下的所有文件和目录。它返回一个三元组(root, dirs, files),其中root是当前目录的路径,dirs是当前目录下的子目录列表,files是当前目录下的文件列表。下面是一个os.walk()的详细解释和示例:

for root, dirs, files in os.walk(top, topdown=True, onerror=None, followlinks=False):
    # Do something with root, dirs, and files

top是指定的目录路径,可以是相对路径或绝对路径。

  • topdown是一个布尔值,表示遍历时是否先遍历当前目录,再遍历子目录。如果为True(默认值),则先遍历当前目录,再遍历子目录;如果为False,则先遍历子目录,再遍历当前目录。

  • onerror是一个可选的错误处理函数,如果在遍历过程中出现错误,则会调用这个函数。

  • followlinks是一个布尔值,表示是否跟随符号链接。如果为True,则会跟随符号链接遍历目录;如果为False(默认值),则会忽略符号链接。

在遍历过程中,os.walk()会依次遍历指定目录及其子目录下的所有文件和目录,并返回当前目录的路径、子目录列表和文件列表。可以通过遍历返回的三元组来处理目录和文件。例如,可以使用下面的代码列出指定目录下的所有文件和子目录:

import os
 
def list_files_and_dirs(path):
    for root, dirs, files in os.walk(path):
        print(f'Directory: {root}')
        for file in files:
            print(f'  File: {os.path.join(root, file)}')
        for dir in dirs:
            print(f'  Subdirectory: {os.path.join(root, dir)}')
 
list_files_and_dirs('.')

这段代码会遍历当前工作目录及其子目录下的所有文件和目录,并输出相应的信息。

需要注意的是,os.walk()只会遍历当前目录及其子目录下的文件和目录,不会遍历符号链接所指向的文件或目录。如果需要遍历符号链接所指向的文件或目录,需要设置followlinks=True。

以上就是关于“Python中的OS module如何使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注***行业资讯频道。

相关文章: