如何使用Google Colab Python在文件夹中读取/循环,然后将每个文件分配为函数参数

发布于 2025-01-20 03:41:09 字数 479 浏览 4 评论 0原文

我目前正在使用Google Colab,并且已经安装了我的Google Drive。 我在驱动器中有一个文件夹,该文件夹具有多个.csv文件,

例如文件夹名称:dataset

文件夹内容:data1.csv,data2.csv,data3.csv,依此类推,等等,

我想通过文件夹中的每个文件迭代,然后进行制作提交功能参数

这是我的代码,但仍然没有起作用,

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'))

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2025-01-27 03:41:09

您必须使用os.listdir之类的函数迭代文件。这是一个使用此功能的示例,并防御性检查读取的内容是csv文件。我已经使用了Google COLAB的sample_data文件夹,以便代码可重复;您将需要更改dir变量才能指向Google Drive文件夹。

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)

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)
绻影浮沉 2025-01-27 03:41:09
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文