如何使用Google Colab Python在文件夹中读取/循环,然后将每个文件分配为函数参数
我目前正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用
os.listdir
之类的函数迭代文件。这是一个使用此功能的示例,并防御性检查读取的内容是csv
文件。我已经使用了Google COLAB的sample_data
文件夹,以便代码可重复;您将需要更改dir
变量才能指向Google Drive文件夹。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 acsv
file. I've used Google Colab'ssample_data
folder so that the code is reproducible; you will need to change thedir
variable to point to your Google Drive folder.