pythonchdir

os.chdir doesn't change directories


When I ask to watch a TV show it lists my TV shows available to me, but when I ask to go into the chosen TV show to show the season it doesn't seem to chdir. I get it printing what it understood, which is right. It just doesn't actually change to the new dir.

My code as follows (understand = voice to text):

def entertain():
    if 'tv show' in understand:
        tv_show = os.listdir('D:\\TV_Shows')
        speak('tv shows you have available are {0}'.format(tv_show))
        print(tv_show)
        speak("what show would you like ")
        if understand == tv_show:
            changed = os.chdir('D:\\TV_Shows\\' + understand)
            tv_show = os.listdir(changed)
            print(tv_show)
    else:
        if 'Movie' in understand:
            movie = os.listdir('D\\movies')
            print(movie)

while True:
    understand = take_command().lower()
    if 'i want to watch a' in understand:
        entertain()

Solution

  • changed = os.chdir('D:\\TV_Shows\\' + understand)
    tv_show = os.listdir(changed)
    

    chdir doesn't return anything. If it fails it raises an exception. No exception means it's succeeded. This code sets changed = None and then calls os.listdir(None).

    Try this instead:

    os.chdir('D:\\TV_Shows\\' + understand)
    tv_shows = os.listdir()