[英]How to traverse all .txt files in folder?
I have the code to read only one txt file from the folder, but actually, I need to traverse all of them. 我有代码只从文件夹中读取一个txt文件,但实际上,我需要遍历所有这些文件。 And run the same steps for each file and output result. 并为每个文件和输出结果运行相同的步骤。 Since I have 6800 files, which means I cannot combine all of them in one data frame. 由于我有6800个文件,这意味着我不能将它们全部组合在一个数据框中。
data =
pd.read_table("C:path/000007.txt",header=None,delim_whitespace=True)
print(data)
data=data.sort_values(by=4).reset_index(drop = True)
data = data.loc[:,[4,5,6,7]] #data before process
print(data)
data['hcd'] = data[6] * 0.5 - data[4] * 0.5 + data[4]
data_1 = data[['hcd','vcd','x','y']] #midpoint and x,y for two points
#print (data_1)
data['hcd2'] = data['hcd']**2
data['vcd2'] = data['vcd']**2
data['x2'] = data['x']**2
data['y2'] = data['y']**2
and so on..... 等等.....
I have the result as: 我的结果如下:
dis para result
0 248.87 67.70 1
1 218.96 101.64 1
1 2
Name: result, dtype: int64
But this is only one txt result, I want to have all file's result and save in one new txt file like this(also delete the Name: result, dtype: int64): 但这只是一个txt结果,我希望得到所有文件的结果并保存在一个新的txt文件中(也删除Name:result,dtype:int64):
#1(numbers of 1) #0(numbers of 0)
filename1(for example: 000001.txt): 2 5
filename1(for example: 000001.txt): 6 7
...... and so on
I would suggest taking a look at using os.walk to get all the files and directories. 我建议看一下使用os.walk获取所有文件和目录。 It is also handy as it can traverse subfolders as well: 它也很方便,因为它也可以遍历子文件夹:
https://www.tutorialspoint.com/python/os_walk.htm https://www.tutorialspoint.com/python/os_walk.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.