超全!python的文件和目錄操做總結

<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)html

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

<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)linux

<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)web

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>shell

<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)windows

<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)app

<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

相關文章
相關標籤/搜索