3

I'm currently using Google Colab and already mounted my Google Drive. I have a folder inside the drive that has multiple .csv files

e.g. folder name: dataset

folder content: data1.csv, data2.csv, data3.csv, and so on

I want to iterate through every file in the folder, then make the file a function parameter

Here's my code but still didn't work

from google.colab import drive
drive.mount('/content/drive/')

def myfunction(data):
###function detail here###

dir = '/content/drive/dataset'

for files in dir:
  myfunction(pd.read_csv('filename'))

Thank you

2 Answers2

3

You have to iterate over files using a function like os.listdir. Here's an example that uses this function and defensively checks that what is read is a csv file. I've used Google Colab's sample_data folder so that the code is reproducible; you will need to change the dir variable to point to your Google Drive folder.

import pandas as pd
import os

def myfunction(data):
  print(data)

dir = 'sample_data'

for file in os.listdir(dir):
  if file.endswith(".csv"):
    myfunction(file)
Andrew Chisholm
  • 6,362
  • 2
  • 22
  • 41
0
from google.colab import drive
drive.mount('/content/drive')

#os.lis`enter code here`tdir(file_path) 
def myfunction(data):
  print(data)
file_path = 'drive/MyDrive/eeg-feature-generation-master/dataset/original_data'

for file in os.listdir(file_path):
  if file.endswith(".csv"):
    myfunction(file)

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
subjectc-neutral-2.csv
subjectc-relaxed-2.csv
subjectd-neutral-1.csv
subjectb-neutral-1.csv
subjecta-concentrating-2.csv
subjectd-concentrating-1.csv
subjectc-concentrating-2.csv
subjectc-relaxed-1.csv
subjectb-relaxed-2.csv
subjectc-neutral-1.csv
subjecta-relaxed-2.csv
subjectd-relaxed-1.csv
subjectd-neutral-2.csv
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '22 at 10:07