-2

I have a Folder with 3 different csv Files like: car.csv home.csv and company.csv

I have a code where i can read the values from the csv files like: with open(car.csv, 'r') as carData:.....

What i want is: that my code should open the first car.csv and do the stuff (myfunction) and then read the next home.csv file and then the company.csv File.

And the end i want to have 3 different text Files.

What i want is:

inputData = './*csv' So my code can read the file without i do it like inputData='./car.csv'

The second thing i want to have is:

when i have 3 csv files in one directory my code should read the first one and loop through my functions and output a text file. After it it should read the second csv file an do it as the first one

deadshot
  • 8,881
  • 4
  • 20
  • 39
m1711
  • 101
  • 1
  • 8
  • 1
    Ok, what have you tried so far? Please post your code. – James May 05 '20 at 11:22
  • Does this answer your question? [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – luigigi May 05 '20 at 11:25
  • doyou want run same functions for each file or different functions – deadshot May 05 '20 at 11:27
  • what i have so far is: inputFile='./input/car.csv' and my functions which are working fine. But when i am removing the car like: inputFile='./input/*.csv' i get: FileNotFoundError: [Errno 2] No such file or directory: './input/*.csv' – m1711 May 05 '20 at 11:28
  • @komatiraju032 i want to use the same function for the 3 or more csv files. – m1711 May 05 '20 at 11:31
  • @m1711 post the code you have tried – deadshot May 05 '20 at 11:33
  • that is what i all have @komatiraju032 – m1711 May 05 '20 at 11:37
  • @m1711 try with **glob** module `glob.glob('/input/*.csv')` – deadshot May 05 '20 at 11:41
  • i get this error: TypeError: stat: path should be string, bytes, os.PathLike or integer, not list – m1711 May 05 '20 at 11:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213163/discussion-between-komatiraju032-and-m1711). – deadshot May 05 '20 at 11:45

1 Answers1

0

In this code, we are reading the CSV files from directory input_dir_name and apply the function sum_col and writing the output to the same directory with output files name starts with out_ + input file name. Is this, you need or specify the function in comments:

import os

input_dir_name = r"\..\input_directory_path...."

input_csv_names = [file_name for file_name in os.listdir(input_dir_name) if file_name.endswith('.csv')]

def sum_col(file_content):
    file_content = file_content[1:]

    file_content1 = [f.replace('\n', '').split(',') for f in file_content]

    file_content2 = [[int(f1[0]), int(f1[1])] for f1 in file_content1]

    out_column = [sum(f2) for f2 in file_content2]

    return out_column


for file_name in input_csv_names:
    print(f'working on file {file_name}')

    input_file_path = os.path.join(input_dir_name, file_name)
    with open(input_file_path, 'r') as file_obj:
        file_content = file_obj.readlines()

    output_column_value = sum_col(file_content)

    output_column_content = ['col3'] + [str(x) for x in output_column_value]

    output_file_content = []
    for i in range(len(output_column_content)):
        output_text = file_content[i].replace('\n', ',') + output_column_content[i] + '\n'
        output_file_content.append(output_text)

    output_file_path = os.path.join(input_dir_name, 'out_' + file_name)

    with open(output_file_path, 'w') as file_obj:
        for line in output_file_content:
            file_obj.write(line)
Saurabh Kansal
  • 643
  • 5
  • 13