0

I recently ran a simulation that output temporal data in directories numbered 0, 0.1, 0.2, ... , 10.

Within each of these directories there is a text file called rigidMotion which contains a line I want to read.

The line looks like:

centreOfRotation (0.000 0.000 0.000) //

where the numbers vary of course.

I would then like only the numbers to be sent to a text file or a csv file so I can plot them.

Given that its the same line in each file I was trying to use linecache but I'm not sure how to set up the for loop to read each directory.

3
  • Read the files in a Loop, then maybe apply some regex on the content, i.e. ^centreOfRotation (.+) in multiline mode and grab the first group. Or, read the text line-wise and use startswith(). Commented May 31, 2016 at 12:15
  • Great regex would work well. But how do I set up a loop to run through these files? It would be great if I could just go: for (int i = 0; i <=10; i + 0.1) then use open for filename/i but that wouldnt work of course. Commented May 31, 2016 at 12:19
  • @Bruce see my answer below Commented May 31, 2016 at 12:22

2 Answers 2

2

You can use glob to find files according to a pattern, which in your case might be something like */rigidMotion, then iterate over those files and collect the values, outputting them to a new file.

Sign up to request clarification or add additional context in comments.

Comments

1

For the directory reading this worked for me the best in multiple directories:

import os

for subdir, dirs, files in os.walk("your root directory"):
    for file in files:
        print os.path.join(subdir, file)
        #processing the files

For the line processing i would use string trimming if only the numbers change in it.

trimmedString = originalString[a:-b] 

where 'a' is the last index what you want to cut from the beginning, and 'b' is for the ending, in your case:

a = the index of the opening bracket

b = index of the closing bracket

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.