HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Accessing files in a child directory in Python 2.7.x

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
directoryfilespythonchildaccessing

Problem

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.

Solution

I suggest a generator:

def children_files(dir):
    onlyfiles = (f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f)))
    for f in onlyfiles:
        file_path = os.path.relpath(f)
        yield os.path.join(dir, file_path)


It is simpler to write (no append) and faster as files will be read just as needed.

Code Snippets

def children_files(dir):
    onlyfiles = (f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f)))
    for f in onlyfiles:
        file_path = os.path.relpath(f)
        yield os.path.join(dir, file_path)

Context

StackExchange Code Review Q#116165, answer score: 3

Revisions (0)

No revisions yet.