2015-09-26 79 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