简体   繁体   English

如何将几个文件读入python代码并循环遍历它们?

[英]how to read several files into a python code and loop over them?

I am coding in python and have written a code which basically reads ina file takes some information from it does some calculations and then outputs the answers to a new file; 我正在用python编码,并编写了一个代码,该代码基本上读取一个文件,并从文件中获取一些信息,然后进行一些计算,然后将答案输出到一个新文件中。 I have now tried to generalise so that it loops over many files (as I have a catalogue of files I need to go through) by reading there file names in from a file in which they are listed. 现在,我尝试进行概括,以便通过从列出文件的文件中读取文件名来遍历许多文件(因为我需要浏览文件目录)。 However I get this error message IOError: [Errno socket error] [Errno -2] Name or service not known 但是,我收到此错误消息IOError:[Errno套接字错误] [Errno -2]名称或服务未知

I assume its because I am now trying to run my code for several files systematically so here is the bit of code in which I do it: 我以为是因为我现在正在尝试系统地为几个文件运行我的代码,因此这是我要做的一些代码:

f = open('//disk2/ps1/cech/CFHTLenS/cluster_catalogues/field_list')
fields = f.readlines()
f.close()
for W in fields:

    file = open('//disk2/ps1/cech/CFHTLenS/cluster_catalogues/clusters_%s_info.cat' %W)
    data = np.loadtxt(file)
    file.close()


    sig_cl = data[:,3].copy()

    m200 = 10**(0.124 * sig_cl + 12.493)
    np.savetxt('//disk2/ps1/bertbert/z_ref_%s.cat'%W,m200)

I'm not sure why you're getting a socket error from file handling routines, but the most obvious issue is that W will have an end of line character at the end which is likely to cause problems. 我不确定为什么您会从文件处理例程中收到套接字错误,但是最明显的问题是W在结尾处会有一个行尾字符,这很可能会引起问题。

Try: 尝试:

W = W.strip()

as the first line of your for loop. 作为for循环的第一行。

This will remove all leading and trailing spaces from W before you add it to the name of the file to open. 在将其添加到要打开的文件名之前,这将从W删除所有前导和尾随空格。

For looping over a arbitrary sets of files the module fileinput could help you. 为了循环遍历任意文件集,模块fileinput可以为您提供帮助。 It sets up a nice command line interface where you can specify a set of files and easily loop over them. 它设置了一个不错的命令行界面,您可以在其中指定一组文件并轻松地遍历它们。

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

相关问题 如何找到文件,然后在Python中循环遍历它们? - How can I find files and then loop over them in Python? 循环遍历目录中的文件并合并python - Loop over files in a directory and merge them python 如何通过循环读取多个文件作为表(熊猫)并从每个表中选择一列并将 append 放在一起 - How to read several files over a loop as a table (panda) and pick one column from each table and append it together 如何在python中循环几个图像? - How to loop over several images in python? 如何读取一个文件夹中的所有文件并在python中对它们应用一个函数? - How to read all files in one folder and apply a function over them in python? 如何迭代python中的文件并导出多个输出文件 - how to iterate over files in python and export several output files 如何循环遍历文件并在Python中重命名它们 - How to loop through files and rename them in Python 在Python中的for循环中,for循环会创建多个文件,并不断用数据覆盖它们吗? - For loop in a while loop in Python creates several files and keeps overwriting them with data? 如何使用 pandas 在循环中创建变量并读取多个 excel 文件? - How to create variables and read several excel files in a loop with pandas? 如何在Python中运行几个大型apache日志文件的逻辑? - How to run logic over several large apache log files in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM