天天看點

Python3.5對檔案的操作

這是一個python3.5對檔案操作的整理,裡面有複制/改名/覆寫輸入/追加輸入等等。

前提:在E盤的PythonAAA/A的檔案夾下,有一個123.txt

1)覆寫輸入

1

2

3

4

5

<code>&gt;&gt;&gt;</code><code>import</code> <code>os    </code><code>#啟動子產品</code>

<code>&gt;&gt;&gt;f</code><code>=</code><code>open</code><code>(</code><code>"e:/PythonAAA/A/123.txt"</code><code>,</code><code>"w+"</code><code>)    </code><code>#打開目标檔案,w+是以讀寫方式打開,同r+</code>

<code>&gt;&gt;&gt;f.write(</code><code>"我愛北京天安門"</code><code>)    </code><code>#寫入内容就是歌詞</code>

<code>7</code>

<code>&gt;&gt;&gt;f.close()    </code><code>#關閉檔案,這一步至關重要!有開有關。</code>

【補充】如果在改路徑下沒有123.txt這個檔案,那麼這樣就會直接生成123.txt這個檔案。

這種f=open的方式在打開檔案之後一定要close(),不然這個檔案一直會在背景運作,windows下使用ctrl+alt+del下是可以看到這個程序的,如果總覺得自己會忘記close(),可以使用with as方法,上面的句子可以這麼寫:

<code>&gt;&gt;&gt;with </code><code>open</code><code>(</code><code>"e:/PythonAAA/A/123.txt"</code><code>) as f:</code>

<code>f.write(</code><code>"我愛北京天安門"</code><code>)</code>

2)追加輸入

如果是with as方法打開檔案的話, 那麼write預設都是追加輸入,比如

<code>&gt;&gt;&gt; with </code><code>open</code><code>(</code><code>"e:/PythonAAA/A/123.txt"</code><code>,</code><code>"a"</code><code>) as f:</code>

<code>    </code><code>f.write(</code><code>"\n 偉大領袖毛主席 \t 帶領我們向前進"</code><code>)  </code><code>#\n和\s效果自己看</code>

效果如下:

<a href="http://s4.51cto.com/wyfs02/M01/7F/AF/wKiom1corYHztJdSAAAhusfCu0w616.png" target="_blank"></a>

如果要用f=的方法,那麼第一行要f=open("e:/PythonAAA/A/123.txt","a") ,這裡a就是add的意思。

3)檔案改名

如果想要把這個 123.txt改成456.txt,很簡單,一句話就能做到。

<code>&gt;&gt;&gt; </code><code>import</code> <code>os</code>

<code>&gt;&gt;&gt; os.rename(</code><code>"e:/PythonAAA/A/123.txt"</code><code>,</code><code>"e:/PythonAAA/A/456.txt"</code><code>)</code>

這個效果立竿見影。

4)檔案複制

想要把e:/PythonAAA/A/123.txt複制到e:/PythonAAA/B這個檔案夾裡,同樣叫123.txt。

<code>&gt;&gt;&gt; </code><code>import</code> <code>shutil</code>

<code>&gt;&gt;&gt; shutil.copy(</code><code>"e:/PythonAAA/B/123.txt"</code><code>,</code><code>"e:/PythonAAA/A/123.txt"</code><code>)</code>

【補充】這個語句的前提是必須要有B這個檔案夾,不然的話,會報錯。

5)目錄删除

e:/PythonAAA/B/C這個檔案夾不想要了,删除之。

<code>&gt;&gt;&gt; shutil.rmtree(</code><code>"e:/PythonAAA/B/C"</code><code>)</code>

6)檔案删除

e:/PythonAAA/B/456.txt這個檔案不想要了,删除之。

<code>&gt;&gt;&gt;  os.unlink(</code><code>"e:/PythonAAA/B/456.txt"</code><code>)</code>

os.remove效果也一樣,語句也一樣。

7)檔案剪切粘貼

e:/PythonAAA/B/123.txt剪貼粘貼到e:/PythonAAA/A/123.txt。

<code>&gt;&gt;&gt; shutil.move(</code><code>"e:/PythonAAA/B/123.txt"</code><code>,</code><code>"e:/PythonAAA/A/123.txt"</code><code>)</code>

8)綜合練習

要求把e:/PythonAAA/A裡所有.py檔案都複制到e:/ABC/CBA這個檔案夾裡。

6

7

8

9

10

11

12

13

14

<code>&gt;&gt;&gt; os.makedirs(</code><code>"e:/ABC/CBA"</code><code>)    </code><code>#建立/ABC/CBA這個檔案夾,如果是一層檔案夾os.mkdir就行</code>

<code>&gt;&gt;&gt; </code><code>def</code> <code>copyFiles(sourceDir,targetDir):</code>

<code>    </code><code>for</code> <code>files </code><code>in</code> <code>os.listdir(sourceDir):</code>

<code>        </code><code>sourceFile </code><code>=</code> <code>os.path.join(sourceDir,files)</code>

<code>        </code><code>targetFile </code><code>=</code> <code>os.path.join(targetDir,files)</code>

<code>        </code><code>try</code><code>:</code>

<code>            </code><code>if</code> <code>os.path.isfile(sourceFile) </code><code>and</code> <code>sourceFile.find(</code><code>'.py'</code><code>)&gt;</code><code>0</code><code>:</code>

<code>            </code><code>#可以試試不加&gt;0的後果</code>

<code>                </code><code>shutil.move(sourceFile,targetFile)</code>

<code>        </code><code>except</code> <code>FileNotFoundError:</code>

<code>            </code><code>print</code><code>(</code><code>"此檔案夾不存在,請重新檢查!"</code><code>)</code>

<code>            </code> 

<code>&gt;&gt;&gt; copyFiles(</code><code>"e:/PythonAAA/B"</code><code>,</code><code>"e:/ABC/CBA"</code><code>)</code>

要麼無錯誤輸出,要麼就會有多少個.py檔案就會傳回多少次“此檔案夾不存在,請重新檢查!”

 本文轉自 蘇幕遮618 51CTO部落格,原文連結:http://blog.51cto.com/chenx1242/1769866