rafaelleru
rafaelleru

Reputation: 357

os.rename in python return errno 20

I'm writing a little program in python 3 to automatize the ordenation of my downloads folder.

when I run it obtain: NotADirectoryError: [Errno 20] Not a directory: '/home/rafa/Descargas/guiaDocente_IA.pdf' -> '/home/rafa/UGR/IA/'

I have two functions in my program, the first find if file name has a substring "cadena" and rename to a new name for his new folder. The code is:

def compruebaArchivo(cadena, archivo):
    archivo_nuevo=""
    if "cadena" in archivo:
        if "_"+cadena in arch:
            archivo_nuevo = archivo.replace("_"+cadena, '')
        elif "-"+cadena in arch:
            archivo_nuevo = archivo.replace("-"+cadena, '')

    print(archivo_nuevo)
    return archivo_nuevo

I also have a function for walk in the Downloads folder and move some files to a new folder:

def mueveArchivos():
for path, dirs, files in os.walk(Descargas):
    for arch in files:
        #Asignatura TSI
        if "TSI" in arch:
            arch_nuevo=compruebaArchivo("TSI", arch)
            os.rename(Descargas + arch, UGR + "TSI/" + arch_nuevo)
        #Asignatura FBD
        elif "FBD" in arch:
            arch_nuevo=compruebaArchivo("FBD", arch)
            os.rename(Descargas + arch, UGR + "FBD/" + arch_nuevo)
        #Asignatura IC
        elif "IC" in arch:
            arch_nuevo=compruebaArchivo("IC", arch)
            os.rename(Descargas + arch, UGR + "IC/" + arch_nuevo)
        #Asignatura IA
        elif "IA" in arch:
            arch_nuevo=compruebaArchivo("IA", arch)
            os.rename(Descargas + arch, UGR + "IA/" + arch_nuevo)
        #Asignatura AC
        elif "AC" in arch:
            arch_nuevo=compruebaArchivo("AC", arch)
            os.rename(Descargas + arch, UGR + "AC/" + arch_nuevo)
        #Asignatura ALG
        elif "ALG" in arch:
            arch_nuevo=compruebaArchivo("ALG", arch)
            os.rename(Descargas + arch, UGR + "ALG/" + arch_nuevo)

for UGR, and Descargas I have the full path name as follow:

home = os.path.expanduser("~")
Descargas = home + "/Descargas/"
UGR = home + "/UGR/"

All the imports are correct but I can't fix this error.I need some help please.

EDIT. The traceback is:

    Traceback (most recent call last):
  File "scripts/orderUGR.py", line 47, in <module>
    mueveArchivos()
  File "scripts/orderUGR.py", line 37, in mueveArchivos
    os.rename(Descargas + arch, UGR + "IA/" + arch_nuevo)
NotADirectoryError: [Errno 20] Not a directory: '/home/rafa/Descargas/guiaDocente_IA.pdf' -> '/home/rafa/UGR/IA/'

Upvotes: 0

Views: 1929

Answers (1)

jfs
jfs

Reputation: 414139

The error is self-explanatory: NotADirectoryError: [Errno 20] Not a directory: '/home/*/guiaDocente_IA.pdf' -> '/home/*/IA/'

You can't rename a file (guiaDocente_IA.pdf) to a directory ('IA/').

The cause of the error is that compruebaArchivo(cadena, archivo) returns an empty string if "cadena" not in archivo. To fix the error, your code should handle the case when arch_nuevo is empty.

btw, use English for names in your code otherwise it is harder to help you.

Upvotes: 1

Related Questions