打开目录中的所有文件,并将它们分别传递到单独的列表中。

huangapple go评论66阅读模式
英文:

To open all the files from a directory and pass each of them to SEPERATE lists

问题

这是我的当前代码。

my_file = open("/content/txts/txt1.txt", "r")
data = my_file.read()
l1 = clean_data(data)

my_file = open("/content/txts/txt2.txt", "r")
data = my_file.read()
l2 = clean_data(data)

my_file = open("/content/txts/txt3.txt", "r")
data = my_file.read()
l3 = clean_data(data)

my_file = open("/content/txts/txt4.txt", "r")
data = my_file.read()
l4 = clean_data(data)

但我不想一遍又一遍地应用相同的函数。为了为我的每个txt文件创建单独的列表,我尝试了另一种方法:

import os
pathToFolder = '/content/txts'
fileList = os.listdir(pathToFolder)
dataDict = {}
for i in range(len(fileList) - 1):
    with open(fileList[i], "r") as f:
        data = f.read()
        dataDict['l' + str(i)] = clean_data(data)

但我遇到了这个错误。这是我的"txts"文件夹。

英文:

This is my current code.

my_file = open("/content/txts/txt1.txt", "r")
data = my_file.read()
l1 = clean_data(data)

my_file = open("/content/txts/txt2.txt", "r") 
data = my_file.read() 
l2 = clean_data(data)

my_file = open("/content/txts/txt3.txt", "r") 
data = my_file.read() 
l3 = clean_data(data)

my_file = open("/content/txts/txt4.txt", "r") 
data = my_file.read() 
l4 = clean_data(data) 

But I dont want to apply the same functions over and over again.
To create seperate lists for each of my txt file, I have tried an alternative:

import os
pathToFolder = '/content/txts'
fileList = os.listdir(pathToFolder)
dataDict = {}
for i in range(len(fileList)-1):
   with open(fileList[i],"r") as f:
    data = f.read()
    dataDict['l' + str(i)] = clean_data(data)
    f.close()

打开目录中的所有文件,并将它们分别传递到单独的列表中。

This is my txts folder打开目录中的所有文件,并将它们分别传递到单独的列表中。

答案1

得分: 0

尝试使用os.listdir()来列出文件夹中的文件,然后循环遍历该列表。您需要保存您离开的文件位置,以便可以从相同的位置开始。以下代码将从您在文件夹中离开的索引处循环到末尾,并将所有干净的数据保存到一个字典中,其中键的名称类似于您在问题中使用的名称。

import os
fileList = os.listdir(pathToFolder)
dataDict = {}
for i in range(WhereYouLeftOffAt, len(fileList) - 1):
   with open(pathToFolder + '/' + fileList[i], "r") as f:
       data = f.read()
       dataDict['l' + str(i)] = clean_data(data)

注意:上述代码中的WhereYouLeftOffAt应该是您离开的索引位置的值。此代码将遍历文件夹中的文件,以及将每个文件的干净数据存储在dataDict字典中,键的命名方式是以'l'开头并附加索引号。

英文:

Try to use os.listdir() to list the files in the folder than loop through the list. You would need to save which file you left off on so you could start in the same place. The following code would loop from whatever index you left off at in the folder to the end and save all the clean dataset into a dictionary with the keys being names similar to what you used in your question.

import os
fileList = os.listdir(pathToFolder)
dataDict = {}
for i in range(WhereYouLeftOffAt,len(fileList)-1):
   open(pathToFolder + '/' + fileList[i],"r") as f
   data = f.read()
   dataDict['l' + str(i)] = clean_data(data)
   f.close()

huangapple
  • 本文由 发表于 2023年2月9日 03:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定