-1

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()
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2schweet
  • 1
  • 2

1 Answers1

0
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()
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • `help(os.listdir)` tells me `If path is None, uses the path='.'.`, though. – Kelly Bundy Jan 05 '21 at 00:35
  • Ah. That wasn't always the behavior in older versions of Python. What makes you think the directory wasn't changed, then? – John Kugelman Jan 05 '21 at 00:48
  • `'.'` is relative to the working directory, if I'm not mistaken. In other words, `os.listdir()` should list whatever directory you've switched to. – CrazyChucky Jan 05 '21 at 00:52
  • @JohnKugelman my code when I run It with a usr = input() instead It works flawlessly Must be something with Other lines I will need to Check it Out There is no need for the chdir to be there Thank you for your help – 2schweet Jan 05 '21 at 02:00
  • It might be worth mentioning that this can be done without changing the working directory. – AMC Jan 05 '21 at 02:04
  • What makes me think it wasn't changed? I don't even know what they mean with that, as they're not exactly telling us how they determine that. My best guess is that they just don't go into the block of `if understand == tv_show:`, as `understand` is a string and `tv_show` is a list, so they're never equal. And they're trying to sell that to us as "chdir doesn't work". Then again, they accepted this answer, so what do I know :-) – Kelly Bundy Jan 05 '21 at 03:13
  • @KellyBundy as stated before I went ahead and changed to your suggestion from == to in same thing gives me the same results – 2schweet Jan 05 '21 at 03:48