Python Forum
getting the length of a file in a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting the length of a file in a loop
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() 

    # 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;

name = "F01_C05.mat"
sio.savemat(name, {'vel':vel})
n = "length.mat"
sio.savemat(n, {'l':l})
Larz60+ write Aug-23-2021, 05:09 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make a test data file for the full length of definition? MDRI 6 1,090 Apr-16-2021, 01:47 AM
Last Post: MDRI
  Python offset to length write in a File basic_learner 3 1,897 Dec-13-2018, 06:20 PM
Last Post: Gribouillis
  Python - Limit Sentence Length to 10 Words - Text file dj99 2 3,077 Jul-21-2018, 02:24 PM
Last Post: dj99

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020