Home > Enterprise >  Looping a function through multiple files in a folder using Python
Looping a function through multiple files in a folder using Python

Time:05-10

I have multiple .csv files in a folder named the following way:

parts-1.csv 
parts-2.csv
.
.
parts-104.csv 

(each file contains only a single column)

I am trying to get the following translator function in Python to go through each of these files, translate and save them as a new file.

data = pd.read_csv("parts4.csv")
translator = Translator()
translations = {}
for column in data.columns:
    unique = data[column].unique()
    for element in unique:
        translations[element] = translator.translate(element).text
for i in translations.items():
    print(I)

I am also trying to save the translated files in the following format:

parts-1-translated.csv 
.
.
parts-104-translated.csv

CodePudding user response:

I will recommend you to read the documentation of pathlib.Path

Example:

from pathlib import Path

BASE_PATH = Path(...)

RECORDS_PATH = BASE_PATH / "records"

for file in RECORDS_PATH.iterdir():
    if not file.is_file():
        continue
    data = file.read_text() # or read_bytes()
    # Translate things
    translated_file = file.with_name(f"{file.stem}-translasted.csv")
    translated_file.write(data)    

CodePudding user response:

import glob, os

for filename in glob.iglob('./**', recursive=True):
    if os.path.isfile(filename): # filter dirs
        print(filename)

From python >= 3.5 onward, you can use ** in glob to get all subdirs.

This will output something like:

./records/parts-1.csv
./records/parts-2.csv
  • Related