0

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/'
rafaelleru
  • 367
  • 1
  • 2
  • 19
  • What is the traceback? – zondo Mar 05 '16 at 12:32
  • `Traceback (most recent call last): File "scripts/orderUGR.py", line 47, in 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/' ` @zondo – rafaelleru Mar 05 '16 at 12:33
  • Please paste it, formatted correctly, in your question. – zondo Mar 05 '16 at 12:36
  • okay @zondo I formated correctly the traceback in the question – rafaelleru Mar 05 '16 at 12:40
  • Your first function does nor work as expected, it seems to return an empty filename. This results in a target filename ending on `/`. The cause might be that the condition is not met for one or more files. – Klaus D. Mar 05 '16 at 12:41
  • 1
    Instead of using `".../" + "..."` etc., use `os.path.join("...", "...")`. It's safer. – zondo Mar 05 '16 at 12:45
  • Does these folders exists? Otherwise you'll have to use `mkdir` to create them. Also, have you tried using double slashes? – dot.Py Mar 05 '16 at 13:45
  • @zondo, what do you mean by safer? why `.join()` is safer than concatening strings using sum operatos? – dot.Py Mar 05 '16 at 13:47
  • 1
    @Dot_Py: Because it isn't just normal `.join()`. `os.path.join()` uses `os.pathsep` instead of always using `/`. Different operating systems have different ways of separating paths, and it isn't always `/`. Therefore, `os.path.join()` is safer because it always uses the right one. Also, `os.path.join()` will normalize paths, so `os.path.join("this/", "that")` comes up with the same result as `os.path.join("this", "that")`: `"this/that"`. – zondo Mar 05 '16 at 13:52

1 Answers1

1

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670