1

tengo un código que utiliza paths y no cambia el directorio actual al utilizar cd debería cambiar de directorio usando chdir() pero esto no ocurre.

import os

Path = os.getcwd()

while True:
    Command = input(f"{Path} - >")
    if Command == "quit":
        quit()
    elif "cd" in Command:
        if len(Command) >= 5:
            CDCommand = Command.split(' ')
            os.chdir(CDCommand[1])
            print(Path)

1 respuesta 1

1

El error está en que nunca actualizas el valor de Path, por lo que pareciera estar siempre en el mismo directorio.

Esta es la versión corregida:

while True:
    Path = os.getcwd()
    Command = input(f"{Path} - >")
    if Command == "quit":
        quit()
    elif "cd" in Command:
        if len(Command) >= 5:
            CDCommand = Command.split(' ')
            os.chdir(CDCommand[1])

produce:

/home/candid/.config/JetBrains/PyCharmCE2021.2/scratches - >/home/candid
/home/candid/.config/JetBrains/PyCharmCE2021.2/scratches - >cd /home/candid
/home/candid - >cd /home/candid/inter
/home/candid/inter - >quit

Process finished with exit code 0
1
  • Vaya error mas tonto que he provocado (siempre que uso ciclos while me pasa algo parecido)
    – dev-t4d3
    el 18 dic. 2021 a las 22:23

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.