0

I am writing a program to take input from a user and print out the contents of the cwd, to be able to change the cwd you're working in, to open a text file in the cwd, or to quit the program.

So far, I can get the dir command to list the .txt files in the cwd but its on an endless loop. I can specify a new directory, but the question just repeats itself, and I can get the program to respond to the quit command.

Here is my code:

import os
cwd = os.getcwd()
print("Current working directory: ", cwd)
dirlist = input("Choose DIR, CD, OPEN, or QUIT: ")

while True:
    if dirlist == "dir":
        for x in os.listdir():
            if x.endswith(".txt"):
                print(x)
    elif dirlist == "cd":
        for x in os.listdir():
            changedir = input("Change to what path?")
            print(cwd)
    elif dirlist == "open":
        for x in os.listdir():
            opendir = input("Open what file?")
            open(opendir)
    else:
        print("Ok, goodbye.")
        break

What do I need to do to accomplish these three things:

  1. not have an endless loop with the dir input and just loop back to the initial question.
  2. take input for a new directory and loop back to the initial question while staying in the new directory.
  3. open a text editor after the open input.

Thanks for any advice.

Edit:

Thanks to people in the comments I have solved questions 1 and 3, but question 2 is still stumping me. Here is my current code-

elif dirlist == "cd":
        for x in os.listdir():
            changedir = input("Change to what path?")
            os.chdir(cwd)
            print (changedir)
            break

How do I get the cwd to show up and print as what it was renamed with the input from changedir?

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
colewalk
  • 3
  • 3
  • If you want to be asking the question repeatedly, then your `while` loop should include those first three lines of code (after the `import`), don't you think? And, by the way, you are not actually changing directories, nor are you opening an editor. `open` just opens a handle to the file, and since you don't save it, it will immediately close. – Tim Roberts Apr 26 '22 at 03:37
  • Tim Roberts, Thank you. Your comment solved my first issue. Sorry, I'm super new to python and just trying to get my feet wet. – colewalk Apr 26 '22 at 03:40
  • For question 2, if I understand correctly, you can use the keyword `continue` which restarts the loop from the top. Also for 3 like the above comment mentions, to open that file you will actually need to do something that can run on the command line, see the answer here: https://stackoverflow.com/questions/6178154/open-a-text-file-using-notepad-as-a-help-file-in-python – Jamalan Apr 26 '22 at 03:53
  • Jamalan- Thank you, I was able to solve question 3 with your help get closer on question 2. My question about two now, is how do I go about having the new path entered be the path that gets printed during the print(cwd) – colewalk Apr 26 '22 at 04:26
  • So you want to take input one more time after the directory has been changed? Or do you want it to continously go on? – Batselot Apr 26 '22 at 05:01
  • Batselot I want to be able to continue on and list/open files in the new directory. – colewalk Apr 26 '22 at 05:04

0 Answers0