Ati Sanghera Portfolio

Data Scientist & Web Developer

File Iterator

This Python code iterates Python commands over multiple files in a folder.

This example also adds the name of the file into a dataframe.

Code

Import required modules

import pandas as pd
import os

Set source path and collect file names

files = os.listdir("your_source_path")

Create dataframe with a column for file names

df = pd.DataFrame([], columns = ['name'])

Iterate over the files

for file in files:
    file_path = os.path.join(path,file)

    # Open the file
    f = open(file_path, "r")
    # Do what you want with the file here :)
    f.close()

    # Add file name to dataframe
    df = df.append({'name': file}, ignore_index=True)

Export the dataframe into a CSV file

df.to_csv ("your_destination_path", index = False, header=True)