Python read files in directory and concatenate
- by JJ Beck
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?