Mikey9191
Mikey9191

Reputation: 7

Read csv files from multiple folders using a for loop

My code will read from a csv file and perform multiple operations/calculations then create another csv file, i have 8 folders to read/write from and i want my code to iterate through them one by one

lets say i have folders named Folder1 to Folder8, first of all how do i specify my code to read from a different directory instead of the default one where the python script exists?

this is part of my code

#read the columns from CSV
MAXCOLS = Number_Of_Buses + 1
Bus_Vol = [[] for _ in range(MAXCOLS)]
with open('p_voltage_table_output.csv', 'rb') as input:
    for row in csv.reader(input, delimiter=','):
        for i in range(MAXCOLS):
            Bus_Vol[i].append(row[i] if i < len(row) else '')

for i in xrange(1,MAXCOLS):
    dummy=0
    #print('Bus_Vol[{}]: {}'.format(i, Bus_Vol[i]))

i want to be able to specify the directory folder to folder1 and also iterate through folder1 to folder8 which all have the same csv file with the same name

Upvotes: 0

Views: 3256

Answers (1)

Lost
Lost

Reputation: 1008

To read a directory other than where your script is located, you need to provide python the absolute path to the directory.

Windows style: c:\path\to\directory

*nix style: /path/to/directory

In either case it'll be a string.

You didn't specify if your target folders were in the same directory or not. If they are, it's a bit easier.

import os
path_to_parent = "/path/to/parent"

for folder in os.listdir(path_to_parent):
    for csv_file in os.listdir(os.path.join(path_to_parent, folder)):
        # Do whatever to your csv file here

If your folders are spread out on your system, then you have to provide an absolute path to each one:

import os
paths_to_folders = ['/path/to/folder/one', '/path/to/folder/two']

for folder in paths_to_folders:
   for csv_file in os.listdir(folder):
        # Do whatever to your csv file

Upvotes: 1

Related Questions