简体   繁体   English

如何在具有不同名称的文件夹中加载 all.txt 文件 - Python

[英]How to load all .txt files in a folder with separate names - Python


I have to create a bunch of numpy arrays by importing all the.txt files from a folder.我必须通过从文件夹中导入所有 the.txt 文件来创建一堆 numpy arrays。 This is the way I'm doing it right now:这就是我现在正在做的方式:

wil_davide_noIA_IST_nz300           = np.genfromtxt("%s/davide/wil_davide_noIA_IST_nz300.txt" %path)
wig_davide_multiBinBias_IST_nz300   = np.genfromtxt("%s/davide/wig_davide_multiBinBias_IST_nz300.txt" %path)
wig_davide_multiBinBias_PySSC_nz300 = np.genfromtxt("%s/davide/wig_davide_multiBinBias_PySSC_nz300.txt" %path)
wig_davide_noBias_PySSC_nz300       = np.genfromtxt("%s/davide/wig_davide_noBias_PySSC_nz300.txt" %path)
wig_davide_IST_nz300                = np.genfromtxt("%s/davide/wig_davide_IST_nz300.txt" %path)
wig_davide_noBias_IST_nz300         = np.genfromtxt("%s/davide/wig_davide_noBias_IST_nz300.txt" %path)
wig_davide_PySSC_nz300              = np.genfromtxt("%s/davide/wig_davide_PySSC_nz300.txt" %path)
...

from the folder从文件夹

在此处输入图像描述

Can I automate the process somehow?我可以以某种方式自动化这个过程吗? Note that I'd like the array to have the same name as the imported file (without the.txt, of course).请注意,我希望数组与导入的文件具有相同的名称(当然,没有 .txt)。
Thank you very much非常感谢

This is how you can get all the filenames.这是获取所有文件名的方法。

path is whatever is your file path ( "./" if in the same directory) path是您的文件路径(如果在同一目录中,则为"./"

import os

path = "./"

filenames = []
for filename in os.listdir(path):
  filenames.append[file]

...

Also, you can filter some files with the if-structure provided.此外,您可以使用提供的 if 结构过滤一些文件。 Here I'm selecting only txt files.这里我只选择 txt 文件。

filter is some filter you can apply (use "." not in file to get directories) filter是您可以应用的一些过滤器(使用"." not in file中获取目录)

import os

path = "./"
filter = ".txt"

txt_filenames = []
for filename in os.listdir(path):
  if filter in file:
    txt_filenames.append[filename]

...

A heads up: You should not do this, if there are any security concerns with the files or their names.注意:如果文件或其名称存在任何安全问题,您不应该这样做。

Otherwise you can use the exec function to get the variables you want.否则,您可以使用exec function 来获取您想要的变量。 For a list approach see the other solution.有关列表方法,请参阅其他解决方案。

import os
path_to_folder = "."
command = ""
for f in os.listdir(path_to_folder):
    if f.endswith(".txt"):
        command += f.replace(".txt", "") + f" = np.genfromtxt('{path_to_folder}/{f}')\n"
exec(command)

Depended on your folder/file structure you would need to change up the code a bit, but that should work like asked.根据您的文件夹/文件结构,您需要稍微更改代码,但这应该像要求的那样工作。

I haven't tested this, but I think this pseudo-code should work - the only thing "pseudo" about it is the hardcoded "dir/to/files" string, which you'll have to change to the path to the directory containing the text files.我没有对此进行测试,但我认为这个伪代码应该可以工作 - 关于它的唯一“伪”是硬编码的"dir/to/files"字符串,您必须将其更改为目录的路径包含文本文件。 In modern Python you would use the pathlib or glob standard library modules to iterate over all text files in a given directory.在现代 Python 中,您将使用pathlibglob标准库模块来遍历给定目录中的所有文本文件。 Creating a variable number of variables, with variable names determined at runtime is never a good idea.创建可变数量的变量,并在运行时确定变量名称绝不是一个好主意。 Instead, keep your numpy arrays in some kind of collection.相反,请将您的 numpy arrays 保存在某种集合中。 In this case, I would suggest a dictionary, so that you can access the individual numpy arrays by key, where the key is the name of the corresponding file as a string - without the extension of course:在这种情况下,我建议使用字典,以便您可以通过键访问单个 numpy arrays ,其中键是相应文件的名称作为字符串 - 当然没有扩展名:

def get_kv_pairs():
    from pathlib import Path
    for path in Path("dir/to/files").glob("*.txt"):
        yield path.stem, np.genfromtxt(str(path))
        

arrays = dict(get_kv_pairs())

print(arrays["wil_davide_noIA_IST_nz300"]) # Here's how you would access the individual numpy arrays.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python和bs4读取和覆盖文件夹中的所有* .txt文件? - How to read and overwrite all *.txt files in a folder with python and bs4? Python将文件夹中的所有csv文件转换为txt - Python Convert all csv files in folder to txt 如何从需要在单独文件夹中读写 .txt 文件的 .py 文件创建 python 可执行文件 - How to create python executable from a .py file that need to read and write .txt files in a separate folder 如何使用 Python 将文件夹中的 all.txt 文件和 append 其内容读取到 one.txt 文件中? - How to read all .txt files in a folder and append its contents into one .txt file, using Python? 从python中的文件夹中读取多个.txt文件的名称 - Reading names of multiple .txt files from a folder in python 如何遍历文件夹中的所有.txt文件? - How to traverse all .txt files in folder? 如何在python中读取文件夹中的txt文件列表 - how to read a list of txt files in a folder in python 如何将txt文件的文件夹另存为python中的变量 - How to save a folder of txt files as a variable in python 使用python加载和读取文件夹中的所有文件json - Load and read all files json in a folder with python 如何在 python 中加载 .txt 文件进行循环? - How to load .txt files in python for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM