I have written the below script for use in Python 2.7.x. In essence, I want the script to access a folder contained within the directory the script is and then add all files contained within to a list. I'm then looking to open these files using the csv module and process each line for now.
My script works fine as below but it just seems to be a long-winded way to access the content of files stored in a child directory.
Any suggestions to improve it?
import os import csv child_files =  mypath = "./child_directory/" onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))] for f in onlyfiles: file_path = os.path.relpath(f) x = os.path.join(mypath, file_path) child_files.append(x) print x for f in child_files: with open(f, 'rb') as x: reader = csv.reader(x) for row in reader: print row
I'd also be keen to only add '.csv' files contained within the child directory. I can work on this upon a review of my script unless someone can suggest a pythonistic method of incorporating this.