I have a directory in which there are files with the extension .dat.
How can I quickly convert all files in this directory to .mat expansion.
Asked
Active
Viewed 1,441 times
-1

Rory Daulton
- 21,934
- 6
- 42
- 50

SoH
- 49
- 7
-
1Convert or rename? – Bogdan Doicin Jun 04 '19 at 18:33
-
@BogdanDoicin Convert – SoH Jun 04 '19 at 18:33
-
Are you sure the .dat files have a format which will be read by Matlab? Because renaming is easy – Bogdan Doicin Jun 04 '19 at 18:34
-
1@SoH: How can we help you convert the files if we don't know what format they are? There is no standard `.dat` format. We have no idea what's in them. – gnovice Jun 04 '19 at 19:19
-
@gnovice How to see what is stored in them? – SoH Jun 04 '19 at 19:36
1 Answers
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