0

I'm trying to perform some mathematical functions on the values in the arrays. But I want to do it for more than one file, since it's tedious to use loadtxt for each file. Since each .txt file contains three columns, I want to assign t, x, y as the arrays of those columns. I'm inexperienced in this. I used os.chdir since I'm changing the directory to the files in a specific folder, the problem is defining the arrays.

os.chdir(r"F:\Thermal Motion")
files = dir('*.txt')
for i in range(len(files)):
    t, x, y = loadtxt(files(i))

2 Answers 2

1

You could use unpack=True parameter, to unpack the result into separate t, x, y arrays:

from glob import glob
import numpy as np

for path in glob(r"F:\Thermal Motion\*.txt"):
    t, x, y = np.loadtxt(path, unpack=True)
    use_the_arrays_here(x, y, t) # <--  use mathematical functions here
Sign up to request clarification or add additional context in comments.

2 Comments

what does the use_the_arrays_here(x,y,t) do, since I get error that this is not defined
@Kimi, that's just an example, to show where you would do other things with x, y, and t. It's where you would "perform some mathematical functions on the values in the arrays."
0

You can also try using the csvreader library to do a lot of the work for you, if that is allowed by your problem.

Also there's not really a need to use chdir if you know the path to the file, just pass the path directly to the open call, or combine the directory and the filename with the following:

full_path = os.path.join(dir_path, filename)

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.