超全!python的文件和目录操作总结

2019/04/10 10:10
阅读数 15

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;"><h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">文件的基本读写</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">path = <span class="hljs-string" style="color: #98c379; line-height: 26px;">r'C:\Users\Brady\Documents\tmp'</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> open(path + <span class="hljs-string" style="color: #98c379; line-height: 26px;">r'\demo.txt'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'r'</span>, encoding=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'utf-8'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> f: content = f.read() print(content) </code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>open()函数</span></h3> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; border-left: 3px solid rgba(0, 0, 0, 0.4); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(239, 112, 96); background: #fff9f9;"> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0px; color: black; line-height: 26px;"><strong style="font-weight: bold; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)</code></strong> Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.</p> </blockquote> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">open函数用于打开一个文件,并返回文件句柄.</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">文件打开的mode主要有以下几种方式:</p> <table data-tool="mdnice编辑器" style="display: table; text-align: left;"> <thead> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0;">mode</th> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0;">含义</th> </tr> </thead> <tbody style="border: 0;"> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'r'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">读取(默认)</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'w'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">写入(会截断之前的文件内容)</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'x'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">写入(如果文件已经存在会产生异常)</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'a'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">追加,将新内容写入到文件末尾</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'b'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">二进制模式</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'t'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">文本模式(默认)</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">'+'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">更新,可读可写</td> </tr> </tbody> </table> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">这里关于newline做一个解释. newline是换行符,windows系统的换行符和类unix系统的换行符是不一样的. windows默认使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\r\n</code>做为换行符. 而类unix系统使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\n</code>作为换行符.</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">关于换行符的使用,文档给出了如下解释:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">如果newline为None,则<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\r</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\n</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\r\n</code>都会被识别为换行符,并统一翻译为<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\n</code>.</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">如果newline为'',则直接返回源文件中的换行符</section></li></ul> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; border-left: 3px solid rgba(0, 0, 0, 0.4); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(239, 112, 96); background: #fff9f9;"> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0px; color: black; line-height: 26px;">关于换行符<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\r\n</code> 和<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\n</code> 的历史要追溯到计算机出现之前的电传打印机. <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\r</code>的意思代表回车,也就是打印头回到初始位置. <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\n</code>的意思表示换行,也就是纸张往上卷一行. 在windows中保留了这种老传统. 真正的换行符需要<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\r\n</code> 而类unix中则选择使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">\n</code>作为换行符</p> </blockquote> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>write()函数</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> open(path+<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'\demo2.txt'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'w'</span>,encoding=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'utf-8'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> f: content = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'this is a demo for write function'</span> res=f.write(content)

print(res) </code></pre>

<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>file对应的方法</span></h3> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.close(): 关闭文件</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.flush():讲缓冲区的内容立即写入文件</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.readline():读取整行</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.readlines():按行读取,并返回列表.可以设定读取的字节数</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.seek()设置游标位置</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.tell()显式当前游标位置</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">file.truncate()截取文件</section></li></ul> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">目录相关操作</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>获取目录列表</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> os.scandir(path2) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> entries: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> item <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> entries: print(item.name) </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">scandir()</code>返回的是一个生成器.</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">同样也可以使用pathlib库.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">enties = Path(path2) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> enties.iterdir(): print(entry.name) </code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>获取目录下的文件</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.listdir(basepath): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> os.path.isfile(os.path.join(basepath,entry)): print(entry)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> os.scandir(basepath) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> entries: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> entries: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> entry.is_file(): print(entry.name)

base_path = Path(basepath) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> base_path.iterdir(): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> entry.is_file(): print(entry.name)

base_path = Path(basepath) files_in_basepath = (entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> base_path.iterdir() <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> entry.is_file()) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> item <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> files_in_basepath: print(item.name) </code></pre>

<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">以上四种办法都可以.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>获取子目录</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.listdir(basepath): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> os.path.isdir(os.path.join(basepath,entry)): print(entry)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> os.scandir(basepath) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> entries: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> entries: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> entry.is_dir(): print(entry.name)

base_path = Path(basepath)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> base_path.iterdir(): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> entry.is_dir(): print(entry.name) </code></pre>

<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>获取文件属性</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> os.scandir(basepath) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> entries: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> entries: info = entry.stat() print(entry.name,timestamp2datetime(info.st_mtime))

base_path = Path(basepath)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> entry <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> base_path.iterdir(): info = entry.stat() print(entry.name,timestamp2datetime(info.st_mtime)) </code></pre>

<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">os.scandir()</code>返回一个os.dirEntry对象. os.dirEntry对象大概有以下属性和方法:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">name:文件(目录)名</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">path:文件(目录)路径</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">is_file()</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">is_dir()</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">stat()返回一个stat_result对象.</section></li></ul> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">而stat_result对象又有N多关于文件的属性,比如时间戳相关的属性:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">st_atime:最近访问时间</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">st_mtime:最近修改时间</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">st_ctime:创建时间</section></li></ul> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">创建目录</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">在<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">os</code>和<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">pathlib</code>的模块中都包含了创建目录的函数.</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">os.mkdir() 创建单个子目录</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">os.makedirs() 创建多个目录,包括中间目录</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">Pathlib.Path.mkdir() 创建单个或者多个目录</section></li></ul> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>创建单个目录</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">os.chdir(basepath) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">not</span> os.path.exists(os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'c'</span>)): os.mkdir(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'c'</span>)

base_path = Path(basepath+<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'\d'</span>)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">try</span>: base_path.mkdir() <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">except</span> FileExistsError : <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">pass</span> </code></pre>

<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">通过os.mkdir()和Path.mkdir()都可以创建单个目录. 如果目录已经存在,则会报<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">FileExistsError</code>异常. 也可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">exist_ok=True</code> 参数来忽略这个异常</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>创建多个目录</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">os.makedirs()</code>来创建包含中间目录在内的所有目录,类似mkdir -p</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">os.makedirs(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'2020/3/2'</span>) </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">也可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">Path.mkdir()</code>方法来创建多层目录.只需要指定<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">parents=True</code>比如</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">from</span> pathlib <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> Path p = Path(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'2018/10/05'</span>) p.mkdir(parents=<span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">True</span>, exist_ok=<span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">True</span>) </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">文件名的模式匹配</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>使用字符串方法</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">python有一些内置的修改和操作字符串的方法,在操作文件名的时候,可以先遍历拿到文件名,然后使用字符串的方式进行匹配.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> item <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.listdir(basepath): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> item.endswith(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'.txt'</span>): print(item) </code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>使用fnmatch库</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">另外还可以使用fnmatch库,fnmatch库支持类unix的通配符.</p> <table data-tool="mdnice编辑器" style="display: table; text-align: left;"> <thead> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0;">通配符</th> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0;">含义</th> </tr> </thead> <tbody style="border: 0;"> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">*</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">匹配所有字符</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">?</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">匹配任意一个字符</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">[seq]</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">匹配一个序列</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">[!seq]</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">匹配一个不包含seq的序列</td> </tr> </tbody> </table> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> fnmatch <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> item <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.listdir(basepath): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> fnmatch.fnmatch(item,<span class="hljs-string" style="color: #98c379; line-height: 26px;">"*.txt"</span>): print(item) </code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>使用glob库</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">总的来说,glob库和fnmatch库差不多,但是glob库提供了递归功能,可以查询目录下子目录的文件名. <strong style="font-weight: bold; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">glob.glob(pathname,&nbsp;*,&nbsp;recursive=False)</code></strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">另外在pathlib中也提供了类似glob的方法.</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">总结:</p> <table data-tool="mdnice编辑器" style="display: table; text-align: left;"> <thead> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0;">函数</th> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left; font-weight: bold; background-color: #f0f0f0;">描述</th> </tr> </thead> <tbody style="border: 0;"> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">startswith()</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">是否以一个特定的序列开头</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">endswith()</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">是否以一个特定的序列结尾</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">dnmatch.fnmatch(filename,pattern)</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">测试文件名是否满足正则表达式</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">glob.glob()</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">返回匹配的文件列表</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">pathlib.Path.glob()</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: left;">返回一个匹配该模式的生成器对象</td> </tr> </tbody> </table> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">遍历和处理文件</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">os.walk(top,&nbsp;topdown=True,&nbsp;onerror=None,&nbsp;followlinks=False)</code></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">os.chdir(basepath) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> dirpath,dirname,files <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.walk(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'.'</span>): print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f'found directory:<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{dirpath}</span>'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> filename <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> files: print(filename) </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">walk()方法返回一个三元组(dirpath,dirnames,filenames)</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">dirpath:当前目录的名称</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">dirnames:当前目录中子目录的列表</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">当前目录中文件的列表</section></li></ul> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">创建临时文件和目录</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">临时文件和临时目录就是程序运行时创建,在程序运行结束之后会自动删除的文件和目录. 可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">tempfile</code>模块来进行操作.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">from</span> tempfile <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> TemporaryFile <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">from</span> tempfile <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> TemporaryDirectory

fp = TemporaryFile(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'w+t'</span>) fp.write(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello world'</span>) fp.seek(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>) data = fp.read() print(data) fp.close()

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> TemporaryFile(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'w+t'</span>,encoding=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'utf-8'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> tf: tf.write(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello world'</span>) tf.seek(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>) print(tf.read())

tmp=<span class="hljs-string" style="color: #98c379; line-height: 26px;">''</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> TemporaryDirectory() <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> tmpdir: print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"create a temp directory{0}"</span>.format(tmpdir)) tmp = tmpdir print(os.path.exists(tmp))

print(os.path.exists(tmp)) </code></pre>

<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">临时文件作为一个临时的硬盘上的缓存,一般不需要命名. 但是如果需要使用带文件名的临时文件时,可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">tempfile.NamedTemporaryFile()</code></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">在windows平台下,临时文件一般存放在<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">C:/TEMP</code>或者<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">C:/TMP</code>. 其他平台上,一般存放顺序为<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">/tmp</code>,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">/var/tmp</code>,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">/usr/tmp</code> 如果以上路径都找不到的话,python会默认在当前目录中存放临时文件和临时目录.</p> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; border-left: 3px solid rgba(0, 0, 0, 0.4); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(239, 112, 96); background: #fff9f9;"> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0px; color: black; line-height: 26px;">注意,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">TemporaryFile()</code>等方法也是支持with..in这种上下文管理器的.</p> </blockquote> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">删除文件和目录</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>删除文件</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">要删除单个文件有三种办法:<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">pathlib.Path.unlink()</code> , <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">os.remove()</code> 还有 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">os.unlink()</code>方法</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">这里需要注意的是,os.remove()和os.unlink()没有什么区别. unlink是类unix系统中的早期叫法.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">os.remove(os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo.txt'</span>)) os.unlink(os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo2.txt'</span>)) </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">或者使用pathlink.Path.unlink()方法</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">from</span> pathlib <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> Path p = Path(basepath+<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'\1-demo.txt'</span>) p.unlink() </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">注意,以上方法只能删除文件,如果删除的不是文件而是目录的话,会报<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">IsADirectoryError</code>异常</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>删除目录或目录树</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">三个方法:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">os.rmdir()</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">pathlib.Path.rmdir()</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">shutil.rmtree()</section></li></ul> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">在os.rmdir()和pathlib.Path.rmdir()中,如果删除的是非空目录,会报OSError异常.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">os.rmdir(os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'a'</span>)) p = Path(basepath+<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'\b'</span>) p.rmdir() </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">如果想删除非空目录或者目录树的话,可以是用shutil.rmtree()方法</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">shutil.rmtree(os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'2020'</span>)) </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">复制,移动和重命名文件和目录</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">这里我们要使用到shutil模块,shutil模块提供了类似shell的一些功能.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>复制文件</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> os <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> shutil src = os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'0-demo.txt'</span>) dst = os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'c'</span>) shutil.copy(src,dst) </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">这个不需要多讲了,类似cp命令. 如果dst是文件,则覆盖原文件,如果dst是目录的话,则拷贝到该目录下.</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">copy()方法不会复制元数据. 如果要连文件信息等元数据一起复制的话,则需要使用copy2()方法.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>复制目录</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> os <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> shutil src = os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'c'</span>) dst = os.path.join(basepath,<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'd\bak'</span>)

shutil.copytree(src,dst) </code></pre>

<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">这里需要注意的是,目标目录不能是已存在的目录. 而且在复制的时候,不带原目标目录的父目录. 说人话就是上面这段代码在执行的时候,只会讲c目录内的内容复制到bak目录里去.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>移动文件和目录</span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">import os import shutil src = os.path.join(basepath,'c') dst = os.path.join(basepath,r'd\bak')

shutil.move(src,dst) </code></pre>

<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">跟shell中的mv用法一样一样一样的. 如果目的目录存在,则会将源目录移动到目的目录中去. 如果目的目录不存在,那就是源目录的重命名.</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>重命名文件和目录</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">可是使用os模块中的rename()方法,也可以使用pathlib.Path.rename()方法.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">os.chdir(basepath) os.rename(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'3-demo.txt'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo3.txt'</span>) p = Path(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'0-demo.txt'</span>) p.rename(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo0.txt'</span>) </code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="display: inline-block; font-weight: bold; background: rgb(239, 112, 96); color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">归档</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid #efebe9; border-right: 20px solid transparent;"> </span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">所谓归档就是打包. 最常见的两种打包方式就是zip和tar.(嗯...不要说rar...)</p> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>读取zip文件</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">python提供了zipfile的内置模块用来处理zip文件.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> os <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> zipfile

os.chdir(basepath)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> zipfile.ZipFile(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'d.zip'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'r'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> zf: filelist=zf.namelist() bar_file_info = zf.getinfo(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'d/bak/0-demo.txt'</span>) print(type(bar_file_info)) print(bar_file_info.file_size) print(filelist) </code></pre>

<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>提取zip文件</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">通过zipfile.extract()和zipfile.extractall()可以从zip文件中提取一个或多个文件.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> zipfile.ZipFile(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'d.zip'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'r'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> zipobj: zipobj.extract(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'d/bak/0-demo.txt'</span>)&nbsp;&nbsp;&nbsp; zipobj.extractall(path=<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'./zip/'</span>) </code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>创建新的zip文件</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">直接使用write()方法就可以了.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">file_list = [] <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> item <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.listdir(): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> fnmatch.fnmatch(item,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'*-demo.txt'</span>): file_list.append(item)

<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> zipfile.ZipFile(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo.zip'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'w'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> zipobj: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> txt_file <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> file_list: zipobj.write(txt_file) </code></pre>

<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>tarfile库的操作</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">tar文件在linux中比较常用,可以使用gzip,bzip2和lzma等压缩方法进行压缩. python同样内置了tarfile库用于处理tar文件.</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">file_list = [] <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> item <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> os.listdir(): <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> fnmatch.fnmatch(item,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'*-demo.txt'</span>): file_list.append(item) <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 创建一个tar包</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> tarfile.open(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo.tar.gz'</span>,mode=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'w:gz'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> tf: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> file_name <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> file_list: tf.add(file_name) <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 读取tar包</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> tarfile.open(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo.tar.gz'</span>,mode=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'r:gz'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> tf: <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> member <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> tf.getmembers(): print(member.name) <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 解压缩tar包</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span> tarfile.open(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'demo.tar.gz'</span>,mode=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'r:gz'</span>) <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> tf: tf.extract(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'2-demo.txt'</span>,path=<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'./d/demo'</span>) tf.extractall(path=<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'./d/extractall'</span>) </code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">关于打开模式的解释,懒得翻译了.</p> <table data-tool="mdnice编辑器" style="display: table; text-align: left;"> <thead> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; font-weight: bold; background-color: #f0f0f0; text-align: center;">mode</th> <th style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; font-weight: bold; background-color: #f0f0f0; text-align: center;">action</th> </tr> </thead> <tbody style="border: 0;"> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'r' or 'r:*'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for reading with transparent compression (recommended).</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'r:'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for reading exclusively without compression.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'r:gz'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for reading with gzip compression.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'r:bz2'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for reading with bzip2 compression.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'r:xz'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for reading with lzma compression.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'x' or 'x:'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Create a tarfile exclusively without compression. Raise an FileExistsError exception if it already exists.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'x:gz'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Create a tarfile with gzip compression. Raise an FileExistsError exception if it already exists.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'x:bz2'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Create a tarfile with bzip2 compression. Raise an FileExistsError exception if it already exists.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'x:xz'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Create a tarfile with lzma compression. Raise an FileExistsError exception if it already exists.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'a' or 'a:'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for appending with no compression. The file is created if it does not exist.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'w' or 'w:'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for uncompressed writing.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'w:gz'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for gzip compressed writing.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: white;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'w:bz2'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for bzip2 compressed writing.</td> </tr> <tr style="border: 0; border-top: 1px solid #ccc; background-color: #F8F8F8;"> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">'w:xz'</td> <td style="font-size: 16px; border: 1px solid #ccc; padding: 5px 10px; text-align: center;">Open for lzma compressed writing.</td> </tr> </tbody> </table> <h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: black; font-size: 20px;"><span>shutil库创建存档</span></h3> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">shutil库的make_archive()方法同样可以创建归档. <strong style="font-weight: bold; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">shutil.make_archive(base_name,&nbsp;format[,&nbsp;root_dir[,&nbsp;base_dir[,&nbsp;verbose[,&nbsp;dry_run[,&nbsp;owner[,&nbsp;group[,&nbsp;logger]]]]]]])</code></strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(239, 112, 96);">shutil.unpack_archive(filename[,&nbsp;extract_dir[,&nbsp;format]])</code></strong></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">shutil.make_archive(<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'.\d\backup'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'tar'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'.\d'</span>) shutil.unpack_archive(<span class="hljs-string" style="color: #98c379; line-height: 26px;">r'.\d\backup.tar'</span>) </code></pre> <figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px;"><img src="https://imgkr.cn-bj.ufileos.com/8f67dd0b-d8e9-446c-b696-a3103fef570f.png" alt="吾码2016" style="display: block; margin: 0 auto; width: 100%;"><figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">吾码2016</figcaption></figure> </section>

原文出处:https://www.cnblogs.com/thecatcher/p/12402480.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部