Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to read a file using pandas and then process it. For opening the file I use the following function:

import os
import pandas as pd

def read_base_file(data_folder, base_file):
    files = map(lambda x: os.path.join(data_folder, x), os.listdir(data_folder))
    if base_file in files:
        try:
            df = pd.read_csv(base_file, na_values=["", " ", "-"])
        except Exception, e:
            print "Error in reading", base_file
            print e
            df = pd.DataFrame()

    else:
        print "File Not Found."
        df = pd.DataFrame()
    return df

My main concerns are the if statement and what I should return if there is an error.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Generator expression

I advice using a generator expression instead of map:

map(lambda x: os.path.join(data_folder, x), os.listdir(data_folder))

should become:

(os.path.join(data_folder, x) for x  in os.listdir(data_folder))

Also x should be renamed to something more expressive.

Separation of concerns

You both print and return values, if the printing is for debugging purposes, use logger.log

Specific Exception

If you write:

except Exception, e:

any Exception will be caught, I suggest IOException.

share|improve this answer
    
Should I let try/except handle if the file exists or not ? –  evil_inside Jul 22 at 19:43
    
@evil_inside sure, just remove the printing. IOError will catch file not existing or unreadable. –  Caridorc Jul 22 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.