Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to process all files in a directory, but am only getting one

I have a Python script that is successfully processing single files. I am trying to write a for loop to have it get all the files in a directory that the user inputs. However, it is only processing one of the files in the directory.

The code below is followed by the rest of the script that does the analysis on data. Do I need to somehow close the for loop?

import os

print "Enter the location of the files: "; directory = raw_input()
path = r"%s" % directory

for file in os.listdir(path):
    current_file = os.path.join(path, file)

    data = open(current_file, "rb")

    # Here's an abridged version of the data analysis

    for i in range(0, 10):
        fluff = data.readline()

    Input_Parameters = fluff.split("\t")

    output.write("%f\t%f\t%f\t%f\t%.3f\t%.1f\t%.2f\t%.2f\t%s\n" % (Voc, Isc, Vmp, Imp, Pmax, 100 * Eff, IscErr, 100 * (1 - (P2 / Pmax)), file))
    data.close()
like image 833
Ben Avatar asked Sep 19 '12 15:09

Ben


3 Answers

In general, if something does not work, you can try to get something simpler working. I removed the data analysis part and kept the code below. This works with me. I noticed that if I have a directory in my path, the open will fail. I am not sure this is the case with you as well.

import os
import sys

path = '.'

for file in os.listdir(path):
    current = os.path.join(path, file)
    if os.path.isfile(current):
        data = open(current, "rb")
        print len(data.read())
like image 155
Hans Then Avatar answered Nov 14 '22 11:11

Hans Then


The current code in your answer looks basically OK to me. I noticed that it's writing to the same output file for each of the files it processes, so that may lead you to think it's not processing them all.

like image 28
martineau Avatar answered Nov 14 '22 11:11

martineau


for you debugging:


for file in os.listdir(path):
    current_file = os.path.join(path, file)
    print current_file


check the output of current_file

BTW: are you indent your code by tab?

because there are different indent length in your code.

This is bad style

like image 1
Yueyoum Avatar answered Nov 14 '22 09:11

Yueyoum