Rename file extension to a specific one using Python

Question:

I have many files with a specific extension in a folder and what I want is to change the extension to another one (associated to a special executable program for those files), I have this advanced code.

import glob, os, shutil, os, errno, sys
import subprocess
from glob import glob
from os import getcwd
from os.path import join
from os.path import basename
from subprocess import call

camino = 'C:\\Users\\user\\Desktop\\Dats\\COX\\'
listado = glob(join(camino,'D*','20*','B*','1*'))

for archivo in listado:
    tmp = os.path.split(archivo)
    path = tmp[0]
    log = archivo
    estacion = os.path.basename(tmp[0])

    try:
        print (path)
        os.chdir(path)
        subprocess.call(['C:\\Users\\user\\Desktop\\Dats\\rt_mseed.exe', log]) #programa .exe para pasar los archivos a la extensión en especifico

    except OSError as e:
        if e.errno == errno.ENOTDIR:
             print ('error')
        else:
             print ('Error: %s' % e)

When I run it, it shows me the following error:

C:\Users\user\Desktop\Dats\python listado.py
C:\Users\user\Desktop\Dats\COX\DISK1\2014322\B088
Error: [Error 2] The system cannot find the file specified
C:\Users\user\Desktop\Dats\COX\DISK1\2014323\B088
Error: [Error 2] The system cannot find the file specified
C:\Users\user\Desktop\Dats\COX\DISK1\2014324\B088
Error: [Error 2] The system cannot find the file specified
C:\Users\user\Desktop\Dats\COX\DISK1\2014325\B088
Error: [Error 2] The system cannot find the file specified
C:\Users\user\Desktop\Dats\COX\DISK1\2014326\B088
Error: [Error 2] The system cannot find the file specified

Answer:

I think you should assign camino BEFORE the join(...)

and instead of using getcwd() you should use that camino variable

In this way you will be indicating that those files look for them in the indicated camino

It would look like this:

camino = 'C:\\Users\\user\\Desktop\\Dats\\COX\\DISK1\\2014\\' #dentro de esta carpeta se encuentran los archivos a convertir
listado = glob(join(camino, '*','*','B*','1','1*'))
Scroll to Top