Open and read certain files in a directory; write all text in those files to cells in a .csv
Question: I've read a number of posts that get close to my problem, but I still haven't
been able to figure it out so hopefully you all can help me get there!

I have a directory with thousands of subfolders, each with 1-4 files. I need
to find all the .txt files (there are thousands), open them, and write the
text to individual cells in a .csv.

In each .txt is a single chunk of text (sometimes multiple pages long). I need
each chunk of text to occupy a single cell in my .csv.

When I run my code on a test directory (only 10 folders and about 15 .txt
files) I get no errors, a .csv is created, but all the cells are empty.

I'm running python 2.7 in aptana studio and I'm a novice so I'm excited for
some SO brilliance to bail me out :)

Here is my code so far:

    
    
    import csv
    import os
    
    def get_text():
        with open('out.csv','w') as out_file:
            csv_out = csv.writer(out_file, delimiter=',')
            for root, dirs, files in os.walk('/Users//Desktop/TEXT-TEST'):
                for file in files:
                    if file.lower().endswith('.txt'):
                        with open(file) as f:
                            csv_out.writerows(f.read())
    
    get_text()
    


Answer: Maybe this work.

    
    
    import csv
    import os
    
    def get_text():
        with open('out.csv','wb') as out_file:   #use 'wb' to prevent extra blank line
            csv_out = csv.writer(out_file, delimiter=',')
            for root, dirs, files in os.walk(r'/Users/Desktop/TEXT-TEST'):
                for file in files:
                    if file.lower().endswith('.txt'):
                        filepath = os.path.join(root,file)  # You may got an error with out this line
                        with open(filepath, 'r') as f:
                            content = f.read()
                            csv_out.writerow([content])   # change to wirterow
    
    get_text()
    

