为了账号安全,请及时绑定邮箱和手机立即绑定

python循环文件

python循环文件

qq_遁去的一_1 2021-06-07 18:48:38
我有一个粘贴在下面的python代码。它适用于我需要做的事情。您会注意到我加载了一个转储文件。如何遍历所有具有相同 *.dump 结束模式的转储文件,并让每个新文件只向输出文件添加一列新数据?基本上我想添加一个循环,这样我就不必为每个转储文件手动重新编写代码。from ovito.io import *from ovito.data import *from ovito.modifiers import *import numpy as npnode = import_file("../200eV.dump",multiple_frames = True)# Perform Wigner-Seitz analysis:ws = WignerSeitzAnalysisModifier(    per_type_occupancies = True,     eliminate_cell_deformation = True)ws.reference.load("../../../WS_Ref/ws.dump")node.modifiers.append(ws)# Define a modifier function that selects sites of type A=1 which# are occupied by exactly one atom of type B=2.def modify(frame, input, output):    # Retrieve the two-dimensional Numpy array with the site occupancy numbers.    occupancies = input.particle_properties['Occupancy'].array    # Get the site types as additional input:    site_type = input.particle_properties.particle_type.array    # Calculate total occupancy of every site:    total_occupancy = np.sum(occupancies, axis=1)    # Set up a particle selection by creating the Selection property:    selection1 = (site_type == 1) & (occupancies[:,0] == 0) & (occupancies[:,1] == 0)    output.attributes['Ca_Vac'] = np.count_nonzero(selection1)# Insert Python modifier into the data pipeline.node.modifiers.append(PythonScriptModifier(function = modify))# Let OVITO do the computation and export the number of identified # antisites as a function of simulation time to a text file:export_file(node, "defects_200.txt", "txt",     columns = ['Timestep', 'Ca_Vac'],    multiple_frames = True)
查看完整描述

2 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

试试 python glob 包。


>>> import glob

>>> glob.glob('./[0-9].*')

['./1.gif', './2.txt']

>>> glob.glob('*.gif')

['1.gif', 'card.gif']

>>> glob.glob('?.gif')

['1.gif']


查看完整回答
反对 回复 2021-06-16
  • 2 回答
  • 0 关注
  • 137 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信