'How to batch format files of unknown format using python? [closed]

I have a lot of files that end with .plx , which contains numerical data.

I wanna create a script that:

  1. Goes through all the files in all the sub-folders and automatically change the extension from .plx to .txt
  2. Change the data type from str to float64 (except for the first row)
  3. Load the array using numpy.loadtxt

For a single file that I changed its extension manually it will look like this:

x1, y1 = np.loadtxt('crvcopie.txt', delimiter=' ', unpack=True, skiprows= 1, dtype=str)
x1= x1.astype(np.float64)
y1 = y1.astype(np.float64)
plt.plot(x1,y1)

I can't attach an example here, but I'm looking for general guidance on how to execute these tasks !

Copied sample:

"rdf_na"
0 4.7858086e-17
0.05 8.7815539e-17
0.1 1.6690106e-16
0.15 3.2625349e-16
0.2 6.5081648e-16
0.25 1.317591e-15


Solution 1:[1]

As @jsbueno it will work just fine if you write .plx. So loading multiple files will be something like this:

import numpy as np
import os

myfiles = [myfile for myfile in os.listdir() if myfile.endswith(".plx")]

for myfile in myfiles:
    x, y = np.loadtxt(myfile, delimiter=' ', unpack=True, skiprows= 1, dtype=str)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eln05