Python read files in directory and concatenate

Posted by JJ Beck on Stack Overflow See other posts from Stack Overflow or by JJ Beck
Published on 2012-11-05T04:54:08Z Indexed on 2012/11/05 4:59 UTC
Read the original article Hit count: 135

Filed under:
|

I want to write a Python script that searches all folders in the current directory, looks for all .txt files, and creates a file that is a concatenation of all those files (in any order) in the current directory. If folders have subfolders, it should not search those subfolders. An example is

main_folder
  folder_1
    sub_folder
      file1.txt
    file2.txt
  folder_2
    file3.txt

The script is placed inside main_folder. It should create a file that is a concatenation of file2.txt and file3.txt (in any order) inside main_folder.

My question is: How can I tell Python to traverse through the folders, look for .txt files, without going into the subfolders?

© Stack Overflow or respective owner

Related posts about python

Related posts about file