Python Forum

Full Version: getting the length of a file in a loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ive got a code which processes a load of files and turns the output into a single matlab file.

In this code I now want it to record the length (how many rows) are in each of my 900,000 files, and create a new variable with these values in and then save the output to another matlab file.

I have tried writing this in, but when I run this it does not produce the numbers I expect. Can anybody help?



import inspect

c = 0

out = {}

starts = 0
ends = 900000

vel = np.zeros([ends-starts,3])
points = np.zeros([ends-starts,3])
l = np.zeros([ends-starts,1])

for i in range(starts,ends):

    # File to read
    fname = 'RP_DATA_%04.0f.vtp'%i

    # --- read a vtp file ---
    points = vtk.vtkXMLPolyDataReader() 
    points.SetFileName(fname)
    points.Update()

    # print the arrays out
    data = points.GetOutput()
    point_data = data.GetPointData()
    # print(inspect.getdoc(point_data))

    l[c,:] = len('data')
    vel[c,:] = np.mean(vtk_to_numpy(point_data.GetAbstractArray('Velocity')),axis=0)
    #    points[c,:] = vtk_to_numpy(data.GetPoints().GetData())
    c = c+1;
    print(c/ends)   


name = "F01_C05.mat"
sio.savemat(name, {'vel':vel})
n = "length.mat"
sio.savemat(n, {'l':l})
Please wrap code in python tags. There is a button in the editor for this.

Write for 1 file, not many. Once it is working you can make it work for more than 1 file.

Please explain what you are trying to do. The code doesn't make much sense. For example, what do you think this does?
l[c,:] = len('data')
len('data') returns 4, the length of the str 'data'. That probably isn't what you want.