python之os模块详解
<h1>python中的os模块</h1><h2>前言</h2>
<p>1、<strong>os是operation system(操作系统)的缩写;os模块就是python对操作系统操作接口的封装。os模块提供了多数操作系统的功能接口函数。(OS模块提供了与操作系统进行交互的函数)</strong></p>
<p>2、操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。</p>
<p>3、在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),查找测试用例的Excel文件或者Yaml文件等。</p>
<h2>os函数</h2>
<h3><strong>1、当前路径及路径下的文件</strong></h3>
<p> <span class="cnblogs_code">os.getcwd()</span> :查看当前所在路径。【具体到当前脚本的上一级】</p>
<p> <span class="cnblogs_code">os.listdir(path)</span> :列举path目录下的所有文件。<span style="color: rgba(255, 0, 0, 1)">返回的是列表类型。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
os.getcwd() </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 'D:\\pythontest\\ostest'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.listdir(os.getcwd()) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">['hello.py', 'test.txt']</span></pre>
</div>
<h3><strong>2、绝对路径</strong></h3>
<p> <span class="cnblogs_code">os.path.abspath(path)</span> :返回当前文件位置<span lang="EN-US">的绝对路径。</span></p>
<p><span lang="EN-US"> <span class="cnblogs_code">os.path.realpath(path)</span> :返回当前文件位置的绝对路径。</span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><span lang="EN-US">注意:<span style="color: rgba(0, 0, 0, 1)">如果 <span class="cnblogs_code">os.path.abspath(.)</span> 存在与已被定义的方法中,则返回的绝对路径是调用该方法的模块的绝对路径,而不是方法的绝对路径。</span></span></span></p>
<p>举例1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(os.getcwd())
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.abspath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.abspath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">..</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.abspath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">../..</span><span style="color: rgba(128, 0, 0, 1)">'</span>))</pre>
</div>
<p>运行结果:</p>
<p><img src="https://img2020.cnblogs.com/blog/2289881/202109/2289881-20210926172956229-12859186.png"></p>
<p>举例2:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(os.getcwd())<br>
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.realpath(<span style="color: rgba(128, 0, 128, 1)">__file__</span><span style="color: rgba(0, 0, 0, 1)">))<br>
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.abspath(<span style="color: rgba(128, 0, 128, 1)">__file__</span>))</pre>
</div>
<p>运行结果:</p>
<p><img src="https://img2020.cnblogs.com/blog/2289881/202201/2289881-20220105132902058-1765712509.png"></p>
<h3><strong>3、查看指定文件路径的文件夹路径部分和文件名部分</strong></h3>
<p><span lang="EN-US"><span class="cnblogs_code">os.path.split(path)</span> :将指定文件的路径分解为<span lang="EN-US">(文件夹路径<span lang="EN-US">, 文件名<span lang="EN-US">),<span style="color: rgba(255, 0, 0, 1)">返回的数据类型是<strong>元组</strong>类型</span>。</span></span></span></span></p>
<p><span lang="EN-US"><span lang="EN-US"><span lang="EN-US"><span lang="EN-US">①若文件夹路径字符串最后一个字符是\,则只有文件夹路径部分有值;</span></span></span></span></p>
<p><span lang="EN-US"><span lang="EN-US"><span lang="EN-US"><span lang="EN-US">②若路径字符串中均无\,则只有文件名部分有值。</span></span></span></span></p>
<p><span lang="EN-US"><span lang="EN-US"><span lang="EN-US"><span lang="EN-US">③若路径字符串有\,且不在最后,则文件夹和文件名均有值。且返回的文件名的结果不包含\.</span></span></span></span> </p>
<p><img src="https://img2020.cnblogs.com/blog/2289881/202109/2289881-20210926174801533-1517317620.png"></p>
<h3><strong>4、路径拼接</strong></h3>
<p><span class="cnblogs_code">os.path.join(path1, path2, ...)</span> :将入参的<span lang="EN-US">path进行组合,若其中有绝对路径,则之前的<span lang="EN-US">path将被删除。</span></span></p>
<div class="cnblogs_code">
<pre>os.path.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\Hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">('D:\\pythontest\\ostest', 'Hello.py')</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.split(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">('', '.')</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.split(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">('D:\\pythontest\\ostest', '')</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.split(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">('D:\\pythontest', 'ostest')</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.join(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ostest</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'D:\\pythontest\\ostest'</span><span style="color: rgba(0, 0, 0, 1)">
os.path.join(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'D:\\pythontest\\ostest\\hello.py'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.join(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\b</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\a</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'D:\\pythontest\\a'</span></pre>
</div>
<h3><strong>5、获取路径的文件夹路径部分 </strong></h3>
<p> <span class="cnblogs_code">os.path.dirname(path)</span> :返回<span lang="EN-US">path中的文件夹路径部分,且路径结尾不包含<span lang="EN-US">'\'。【即返回文件的路径(此路径不包含文件名)】</span></span></p>
<div class="cnblogs_code">
<pre>os.path.dirname(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'D:\\pythontest\\ostest'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.dirname(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">''</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.dirname(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'D:\\pythontest\\ostest'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.dirname(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'D:\\pythontest'</span></pre>
</div>
<h3><strong>6、获取路径的文件名</strong></h3>
<p> <span class="cnblogs_code">os.path.basename(path)</span> :返回<span lang="EN-US">path中的文件名。</span></p>
<div class="cnblogs_code">
<pre>os.path.basename(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'hello.py'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.basename(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'.'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.basename(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">''</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.basename(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'ostest'</span></pre>
</div>
<h3><strong>7、查看文件时间</strong></h3>
<p><span lang="EN-US"> <span class="cnblogs_code">os.path.getmtime(path)</span> :返回文件或文件夹的最后修改时间,从新纪元到访问时的秒数。</span></p>
<p><span lang="EN-US"> <span class="cnblogs_code">os.path.getatime(path)</span> :返回文件或文件夹的最后访问时间,从新纪元到访问时的秒数。</span></p>
<p><span lang="EN-US"> <span class="cnblogs_code">os.path.getctime(path)</span> :返回文件或文件夹的创建时间,从新纪元到访问时的秒数。</span></p>
<div class="cnblogs_code">
<pre>os.path.getmtime(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">1481695651.857048</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.getatime(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">1481687717.8506615</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.getctime(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">1481687717.8506615</span></pre>
</div>
<h3><strong>8、查看文件大小</strong></h3>
<p> <span class="cnblogs_code">os.path.getsize(path)</span> :返回文件的大小;若path入参值是一个文件夹路径则返回<span lang="EN-US">0。</span></p>
<div class="cnblogs_code">
<pre>os.path.getsize(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">58L</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.getsize(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">0L</span></pre>
</div>
<h3><strong>9、查看文件是否存在</strong></h3>
<p> <span class="cnblogs_code">os.path.exists(path)</span> :判断文件或者文件夹是否存在,返回True 或 False。【文件或文件夹的名字不区分大小写】</p>
<div class="cnblogs_code">
<pre>os.listdir(os.getcwd())<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">['hello.py', 'test.txt']</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.exists(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">True</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.exists(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\Hello.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">True</span>
<span style="color: rgba(0, 0, 0, 1)">
os.path.exists(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\pythontest\\ostest\\Hello1.py</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">False</span></pre>
</div>
<h3><strong>10、os模块中操作目录以及文件的函数</strong></h3>
<p> <span class="cnblogs_code">os.mkdir(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">文件夹名</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</span> :新建文件夹;入参为目录路径,不可为文件路径;(父目录必须存在的情况下创建下一级文件夹)</p>
<p> <span class="cnblogs_code">os.rmdir(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">文件夹名</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</span> :删除文件夹;入参为目录路径,不可为文件路径</p>
<p> <span class="cnblogs_code">os.remove(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">文件路径</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</span> :删除文件;入参为文件路径,不可为目录路径</p>
<p> <span class="cnblogs_code">os.makedirs(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">路径及文件</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</span> :递归新建文件夹;可以连续创建该文件夹的多级目录</p>
<p> <span class="cnblogs_code">os.path.isdir(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">路径</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</span> :判断入参路径是否为文件夹,返回值为布尔值;是文件夹返回True,不是文件夹返回False</p>
<p> <span class="cnblogs_code">os.path.isfile(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">路径</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</span> :判断入参路径是否为文件,返回值为布尔值;是文件返回True,不是文件返回False</p>
<h3><strong>11、os模块中遍历目录数</strong></h3>
<p><strong>一个<strong>遍历目录数</strong>的函数,它以一种深度优先的策略(depth-first)访问指定的目录。</strong></p>
<div class="cnblogs_code">
<pre>os.walk(top=path,topdown=True,oneerror=None) </pre>
</div>
<ul>
<li>参数 <span class="cnblogs_code">top</span> 表示需要遍历的目录树的路径。</li>
<li><span style="color: rgba(255, 0, 0, 1)"><strong>参数 <span class="cnblogs_code">topdown</span> 默认为 <span class="cnblogs_code">True</span></strong></span> ,表示首先返回根目录树下的文件,然后,再遍历目录树的子目录。 当<span class="cnblogs_code">topdown</span> 的值为 <span class="cnblogs_code">False</span> 时,则表示先遍历目录树的子目录,返回子目录下的文件,最后返回根目录下的文件。</li>
<li>参数 <span class="cnblogs_code">oneerror</span> 的默认值为 <span class="cnblogs_code">None</span> ,表示忽略文件遍历时产生的错误;如果不为空,则提供一个自定义函数提示错误信息,后边遍历抛出异常。</li>
</ul>
<p> <span class="cnblogs_code">os.walk()</span> 函数的返回值是一个生成器(generator),每次遍历的对象都是返回的是一个三元组 <span class="cnblogs_code">(root,dirs,files)</span>:该元组有3个元素,这3个元素分别表示每次遍历的路径名,目录列表和文件列表。</p>
<ul>
<li> <span class="cnblogs_code">root</span> 代表当前遍历的目录路径,string类型。</li>
<li> <span class="cnblogs_code">dirs</span> 代表root路径下的<span style="color: rgba(255, 0, 0, 1)"><strong>所有子目录名称</strong></span>;<span style="color: rgba(128, 0, 0, 1)"><strong>list类型</strong></span>,列表中的每个元素是string类型,代表<strong>子目录名称</strong>。</li>
<li> <span class="cnblogs_code">files</span> 代表root路径下的<strong><span style="color: rgba(255, 0, 0, 1)">所有子文件名称</span></strong>;<span style="color: rgba(128, 0, 0, 1)"><strong>list类型</strong></span>,列表中的每个元素是string类型,代表<strong>子文件名称</strong>。</li>
</ul>
<h4>实例1:</h4>
<p>当前目录结构如下:</p>
<p><img src="https://img2022.cnblogs.com/blog/2289881/202205/2289881-20220505130729987-1985922429.png"></p>
<p> 代码1:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 0, 255, 1)">from</span> os.path <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> join
home_path </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/home</span><span style="color: rgba(128, 0, 0, 1)">"<br></span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (root, dirs, files) <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> os.walk(home_path):
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(root)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(dirs)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(files)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">=</span><span style="color: rgba(128, 0, 0, 1)">"</span> * 50)</pre>
</div>
<p>运行结果:</p>
<div class="cnblogs_code">
<pre>/<span style="color: rgba(0, 0, 0, 1)">home
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">zhang</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">li</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">hai.mp4</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>==================================================
/home/<span style="color: rgba(0, 0, 0, 1)">root
[]
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">2.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">3.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>==================================================
/hoome/<span style="color: rgba(0, 0, 0, 1)">zhang
[]
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">zhang_1.mp4</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">zhang_2.mp4</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">zhang_3.mp4</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>==================================================
/home/<span style="color: rgba(0, 0, 0, 1)">li
[]
[]
</span>==================================================</pre>
</div>
<p>解析:</p>
<p>一共三行:<br>
第1行代表当前遍历的目录,我们称为root目录,<br>
第2行代表root目录下的子目录列表,我们称为dirs子目录列表,<br>
第3行代表root目录下的子文件列表,我们称为files子文件列表,<br>
<span style="color: rgba(255, 0, 0, 1)"><strong>注意</strong></span>:上面的列表为空就代表当前遍历的root目录下没有子目录或者没有子文件。</p>
<p>代码2:遍历home目录下获取所有的目录和文件的绝对路径</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 0, 255, 1)">from</span> os.path <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> join
home_path </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/home</span><span style="color: rgba(128, 0, 0, 1)">"<br></span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (root, dirs, files) <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> os.walk(home_path):
</span><span style="color: rgba(0, 0, 255, 1)">for</span> dir <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> dirs:
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(join(root, dir))
</span><span style="color: rgba(0, 0, 255, 1)">for</span> file <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> files:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(join(root, file))</pre>
</div>
<p>运行结果:</p>
<div class="cnblogs_code">
<pre>/<span style="color: rgba(0, 0, 0, 1)">home
</span>/home/<span style="color: rgba(0, 0, 0, 1)">root
</span>/home/<span style="color: rgba(0, 0, 0, 1)">zhang
</span>/home/<span style="color: rgba(0, 0, 0, 1)">li
</span>/home/<span style="color: rgba(0, 0, 0, 1)">test.txt
</span>/home/<span style="color: rgba(0, 0, 0, 1)">hai.mp4
</span>/home/root/1<span style="color: rgba(0, 0, 0, 1)">.txt
</span>/home/root/2<span style="color: rgba(0, 0, 0, 1)">.txt
</span>/home/root/3<span style="color: rgba(0, 0, 0, 1)">.txt
</span>/home/zhang/<span style="color: rgba(0, 0, 0, 1)">zhang_1.mp4
</span>/home/zhang/<span style="color: rgba(0, 0, 0, 1)">zhang_2.mp4
</span>/home/zhang/zhang_3.mp4</pre>
</div>
<h4><strong>实例2:</strong></h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os<br>
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> walk(path):
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> os.path.exists(path):
</span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<br>
<span style="color: rgba(0, 0, 255, 1)">for</span> root,dirs,names <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> os.walk(path):
</span><span style="color: rgba(0, 0, 255, 1)">for</span> filename <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> names:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.join(root,filename)) <span style="color: rgba(0, 128, 0, 1)"># </span><span style="color: rgba(0, 128, 0, 1)">路径和文件名连接构成完整路径<br></span>
<span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
path </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\\Users\\Administrator\\Desktop\\2017-9-1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
walk(path)</span></pre>
</div>
<p>运行结果:</p>
<div class="cnblogs_code">
<pre>C:\Users\Administrator\Desktop\2017-9-1\2017-9-1<span style="color: rgba(0, 0, 0, 1)">.txt
C:\Users\Administrator\Desktop\</span>2017-9-1\2017-9-<span style="color: rgba(0, 0, 0, 1)">1storage.txt
C:\Users\Administrator\Desktop\</span>2017-9-1<span style="color: rgba(0, 0, 0, 1)">\apk.conf
C:\Users\Administrator\Desktop\</span>2017-9-1\数据采集导入质量统计_2017-09-01<span style="color: rgba(0, 0, 0, 1)">.docx
C:\Users\Administrator\Desktop\</span>2017-9-1\test1\2017-9-1<span style="color: rgba(0, 0, 0, 1)">.txt
C:\Users\Administrator\Desktop\</span>2017-9-1\test2\2017-9-1.txt</pre>
</div>
<h3>12、一些表现形式参数</h3>
<p>os模块中还定义了一组文件、路径在不同操作系统中的表现形式参数。如下:</p>
<div class="cnblogs_code">
<pre>os.sep<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'\\'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.extsep</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'.'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.pathsep</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">';'</span>
<span style="color: rgba(0, 0, 0, 1)">
os.linesep</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">'\r\n'</span></pre>
</div>
<h3>13、获取在进程的控制终端上登录的用户的名称: <span class="cnblogs_code">os.getlogin()</span> (即此时pc登录的用户名)</h3>
<p><strong>参数</strong>:不需要参数</p>
<p><strong>返回类型</strong>:此方法返回一个字符串,该字符串表示在进程的控制终端上登录的用户的名称。</p>
<p>示例:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Python program to explain os.getlogin() method</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> importing os module</span>
<span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Get the name of the user </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> logged in on the controlling</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> terminal of the process. </span>
user =<span style="color: rgba(0, 0, 0, 1)"> os.getlogin()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Print the name of the user</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> logged in on the controlling</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> terminal of the process. </span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(user)</pre>
</div>
<p>运行结果:</p>
<p><img src="https://img2022.cnblogs.com/blog/2289881/202208/2289881-20220804195730777-221834508.png"></p>
<h2>举例</h2>
<p>在自动化测试过程中,常常需要发送邮件,将最新的测试报告文档发送给相关人员查看,所以就需要查找最新文件的功能。</p>
<p>举例:查找文件夹下最新的文件。</p>
<p><img src="https://img2020.cnblogs.com/blog/2289881/202109/2289881-20210926203720084-869384513.png"></p>
<p>代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> new_file(test_dir):
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">列举test_dir目录下的所有文件(名),结果以列表形式返回。</span>
lists=<span style="color: rgba(0, 0, 0, 1)">os.listdir(test_dir)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">sort按key的关键字进行升序排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间,所以最终以文件时间从小到大排序</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">最后对lists元素,按文件修改时间大小从小到大排序。</span>
lists.sort(key=<span style="color: rgba(0, 0, 255, 1)">lambda</span> fn:os.path.getmtime(test_dir+<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\\</span><span style="color: rgba(128, 0, 0, 1)">'</span>+<span style="color: rgba(0, 0, 0, 1)">fn))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取最新文件的绝对路径,列表中最后一个值,文件夹+文件名</span>
file_path=os.path.join(test_dir,lists[-1<span style="color: rgba(0, 0, 0, 1)">])
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> file_path
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">返回D:\pythontest\ostest下面最新的文件</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> new_file(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\system files\\workspace\\selenium\\email126pro\\email126\\report</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
</div>
<p>运行结果:</p>
<p><img src="https://img2020.cnblogs.com/blog/2289881/202109/2289881-20210926204030955-1183896165.png"></p>
<h2> 总结</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python--函数 os.sep :主要用于系统路径中的分隔符</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Windows系统通过是“\\”,Linux类系统如Ubuntu的分隔符是“/”,而苹果Mac OS系统中是“:”</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 常用的os模块命令:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 1、os.name——name顾名思义就是'名字',这里的名字是指操作系统的名字,主要作用是判断目前正在使用的平台,并给出操作系统的名字,如Windows 返回 'nt'; Linux 返回'posix'。注意该命令不带括号。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 2、os.getcwd()——全称应该是'get current work directory',获取当前工作的目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 3、os.listdir(path)——列出path目录下所有的文件和目录名。Path参数可以省略。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 4、os.remove(path)——删除path指定的文件,该参数不能省略。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 5、os.rmdir(path)——删除path指定的目录,该参数不能省略。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 6、os.mkdir(path)——创建path指定的目录,该参数不能省略。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 7、os.unlink() 方法用于删除文件,如果文件是一个目录则返回一个错误</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> os.remove() #删除文件</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.rename() #重命名文件</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.walk() #生成目录树下的所有文件名</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.chdir() #改变目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.mkdir/makedirs() #创建目录/多层目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.rmdir/removedirs #删除目录/多层目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.listdir() #列出指定目录的文件</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.getcwd() #取得当前工作目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.chmod() #改变目录权限</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.basename() #去掉目录路径,返回文件名</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.dirname() #去掉文件名,返回目录路径</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.join() #将分离的各部分组合成一个路径名</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.split() #返回(dirname(),basename())元组</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.splitext() #返回filename,extension)元组</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.getatime\ctime\mtime #分别返回最近访问、创建、修改时间</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.getsize() #返回文件大小</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.exists() #是否存在</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.isabs() #是否为绝对路径</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.isdir() #是否为目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.path.isfile() #是否为文件</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> os.system('command') 会执行括号中的命令,如果命令成功执行,这条语句返回0,否则返回1</span></pre>
</div>
<h2>拓展:os.listdir()与os.walk()两种方式获取文件路径与文件目录下所有子目录和文件的区别</h2>
<h3>实例1:<strong>在一个目录下面只有文件时可以使用 <span class="cnblogs_code">os.listdir()</span> 函数</strong></h3>
<p>test_file文件夹中包含三个文件:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">test_file:
test1.txt
test2.txt
test3.txt</span></pre>
</div>
<p>获取该目录下的每个文件的绝对路径:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
path </span>= r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:\Users\XXN\Desktop\test_file</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> each_file <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> os.listdir(path):
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.join(path,each_file))</pre>
</div>
<p>运行结果:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">C:\Users\XXN\Desktop\test_file\test1.txt
C:\Users\XXN\Desktop\test_file\test2.txt
C:\Users\XXN\Desktop\test_file\test3.txt</span></pre>
</div>
<h3>实例2:<strong>当一个目录下面既有文件又有目录(文件夹),可使用 <span class="cnblogs_code">os.walk()</span> 函数读取里面所有文件</strong></h3>
<p>test_file中既包含文件也包含文件夹:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Test_file:
file1:
test1.txt
test2.txt
test3.txt
file2:
test1.txt
test2.txt
test3.txt
test1.txt
test2.txt
test3.txt</span></pre>
</div>
<p>代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
path </span>= r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:\Users\XXN\Desktop\test_file</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> parent,dirnames,filenames <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> os.walk(path):
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(parent,dirnames,filenames)</pre>
</div>
<p>运行结果:</p>
<div class="cnblogs_code">
<pre>C:\Users\XXN\Desktop\test_file [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">file1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">file2</span><span style="color: rgba(128, 0, 0, 1)">'</span>] [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test1.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test2.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test3.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
C:\Users\XXN\Desktop\test_file\file1 [] [</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test1.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test2.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test3.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
C:\Users\XXN\Desktop\test_file\file2 [] [</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test1.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test2.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test3.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>]</pre>
</div>
<p>解析:</p>
<ul>
<li>parent:列出了目录路径下面所有存在的目录的名称</li>
<li>dirnames:文件夹名</li>
<li>filenames:列出了目录路径下面所有文件的名称</li>
</ul>
<p>获得给定路径下所有的文件路径:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
path </span>= r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:\Users\XXN\Desktop\test_file</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> parent,dirnames,filenames <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> os.walk(path):
</span><span style="color: rgba(0, 0, 255, 1)">for</span> filename <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> filenames:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(os.path.join(parent,filename))</pre>
</div>
<p> </p>
获取每个文件的绝对路径
</div>
<div id="MySignature" role="contentinfo">
去期待陌生,去拥抱惊喜。<br><br>
来源:https://www.cnblogs.com/hls-code/p/14768652.html
頁:
[1]