python循环文件

我有一个粘贴在下面的python代码。它适用于我需要做的事情。您会注意到我加载了一个转储文件。如何遍历所有具有相同 *.dump 结束模式的转储文件,并让每个新文件只向输出文件添加一列新数据?基本上我想添加一个循环,这样我就不必为每个转储文件手动重新编写代码。


from ovito.io import *

from ovito.data import *

from ovito.modifiers import *

import numpy as np


node = 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)


qq_遁去的一_1
浏览 141回答 2
2回答

繁星淼淼

试试 python glob 包。>>> import glob>>> glob.glob('./[0-9].*')['./1.gif', './2.txt']>>> glob.glob('*.gif')['1.gif', 'card.gif']>>> glob.glob('?.gif')['1.gif']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python