-1

I have a directory in which there are files with the extension .dat. enter image description here How can I quickly convert all files in this directory to .mat expansion.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
SoH
  • 49
  • 7

1 Answers1

0

.mat files are files which contain data that can be read by Matlab. If the .dat files have a format that can also be read by Matlab, then your problem is a simple file renaming problem. More precisely, change the extension of all the files in a folder from dat to mat.

The code will be something like this:

# Python3 code to rename multiple 
# files in a directory or folder 

# importing os module 
import os 

# Function to rename multiple files 
def main(): 
    i = 0

    for filename in os.listdir("xyz"): #xyz=the folder which has your files
        dst ="s0" + str(i) + "lre.mat" #I suppose the numbering of the files begins with 0. This is the name pattern I detected from your screenshot
        src ='xyz'+ filename 
        dst ='xyz'+ dst 

        # rename() function will 
        # rename all the files 
        os.rename(src, dst) 
        i += 1

# Driver Code 
if __name__ == '__main__': 

    # Calling main() function 
    main() 
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34