Reputation: 109
Hi So I am using paraview "trace" tool to make a python script. When I read the vtk files, the python script is using a command called "LegacyVTKReader" and then there is the name of all the VTK files that I have opened. something like this:
paraview = LegacyVTKReader( FileNames=
['/home/afshinzkh/Desktop/DEM/Sample 1/paraview0500.vtk',
'/home/afshinzkh/Desktop/DEM/Sample 1/paraview1000.vtk',
'/home/afshinzkh/Desktop/DEM/Sample 1/paraview1500.vtk',
'/home/afshinzkh/Desktop/DEM/Sample 1/paraview2000.vtk',
'/home/afshinzkh/Desktop/DEM/Sample 1/paraview2500.vtk'])
now the problem is in every sample I have, there are different numbers of VTK files with different names. So I want to change the code in a way so that it works for all my samples. more prcisely I want to go to a folder and read all the VTK files in that folder. like this:
paraview paraview = LegacyVTKReader( FileNames=
['/home/afshinzkh/Desktop/DEM/Sample 1/*.vtk'])
is there a way to do that??
Upvotes: 0
Views: 899
Reputation: 818
Here's one way to do what I think you are asking. This will get a list of all files in a folder
from os import listdir
from os.path import isfile, join
# This is done to filter out directories
mypath = "path/to/my/dir"
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
for file in onlyfiles:
paraview paraview = LegacyVTKReader( FileNames= [file])
# do whatever else you do
This assumes you have a folder with only .vtk files. You can of course also easily add checks for that.
A bit cleaner would be to use glob
instead.
import glob
# Returns list of path names that matches
onlyfiles = glob.glob( mypath + '*.vtk')
for file in onlyfiles:
paraview paraview = LegacyVTKReader( FileNames= [file])
# do whatever else you do
Upvotes: 2