2015-09-26 75 views
-1

我非常新的python.I有一個目錄,裏面,我有2子directory.Each子目錄包含100條file.I想讀的每個文件的內容兩個子目錄,並把它們放在一個文本文件中以這樣的方式,每個文件內容是在新file.how一條線,我可以在pyhon.thank實現這一點,你讀取目錄中的所有文件在python

+0

這不是一個代碼寫作服務。顯示你的嘗試,我們會提供幫助。請閱讀[如何提出問題](http://stackoverflow.com/help/how-to-ask) – Pynchia

+0

請告訴我們您到目前爲止所嘗試過的,請! –

回答

1

,因爲你不有什麼..你可以嘗試從這裏開始。您可以使用glob模塊來從單一級別的子目錄加載文件,或者使用os.walk()根據您的要求走任意的目錄結構,

要打開任意嵌套的所有文本文件目錄:

import os 
import fnmatch 

for dirpath, dirs, files in os.walk('Test'): 
    for filename in fnmatch.filter(files, '*.txt'): 
     with open(os.path.join(dirpath, filename)): 
      # deal with this file as next loop will present you with a new file. 
      #use the filename object to do whatever you want with that file 

因爲你的新像你說的。注意縮進。 #Goodluck