5 Replies - 415 Views - Last Post: 28 June 2019 - 12:01 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 745
  • Joined: 15-December 14

Changing directories using a loop

Posted 27 June 2019 - 01:41 PM

Stuck on a program I am currently working on so I am taking a break to clear my head.


Working on a program that uses a loop to change directories till it gets to the directory the user wanted. I wanted the user to be able to say for example, I want to change directories to c: it would change it then loop back and ask if the user wanted to change the current directory again. I haven't solved how to do the second part, yet. I wanted to try and troubleshoot this first problem and then solve how to make the second part work.

I know in theory I could do this with a for loop:
listy = ['a','b','c','d','e']

for i in listy:
	if i == 'c':
		print("thats the letter 'c'")
		break
	else:
		pass

thats the letter 'c'


I can't figure out how to do that changing directories. This was my last attempt.
pick = input("change directory: ")
	import os
	for i in os.chdir("../"):
		if i == os.chdir("{}".format(pick)):
			os.listdir()
			break
		else:
			continue



Is This A Good Question/Topic? 0
  • +

Replies To: Changing directories using a loop

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: Changing directories using a loop

Posted 27 June 2019 - 01:56 PM

I am not understanding. What is changing directories? Your python script exists in a directory. If you want it to access something from a directory then it needs a full path. The program process doesn't actually go anywhere.

So in that extent you can just have it store the 'current path' in a string variable.
Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 745
  • Joined: 15-December 14

Re: Changing directories using a loop

Posted 27 June 2019 - 02:08 PM

Sorry I meant to say current directories.

For example if I open cmd I can key in d: and it will go straight to that directory. I wanted the user to say for example, theyre on their current directory and wanted to change to say, windows. All they had to do was enter windows and the program would change the directory till it got to windows.

Example like this:
Say I wanted to go to the windows directory
c:\users\Albert
cd..
c:\users
cd..
c:
cd windows
c:\windows

I wanted the program to use a loop to change directories till it reached the directory the user wanted.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: Changing directories using a loop

Posted 27 June 2019 - 02:20 PM

Okay.. so again store the 'current' directory in a string.

If the user inputs a command to "move up a directory" then truncate the current directory by the last most back slash.

Handle the use case if there are no more back slashes.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: Changing directories using a loop

Posted 27 June 2019 - 03:27 PM

View Postalbert003, on 27 June 2019 - 10:41 PM, said:

I can't figure out how to do that changing directories.


You change the current directory by calling os.chdir with the name of the directory that you want to change into.

Quote

This was my last attempt.
pick = input("change directory: ")
	import os
	for i in os.chdir("../"):
		if i == os.chdir("{}".format(pick)):
			os.listdir()
			break
		else:
			continue



os.chdir does not return a value (or more precisely it returns None), so neither for i in os.chdir("../") nor i == os.chdir(...) will work. The former will throw an exception because None is not iterable and the latter will always be false unless i were None for some reason.

If you want to change into the directory named by pick, you should simply write os.chdir(pick) and that's it. If you then want to repeat this, you can put a loop around the whole thing, but not one that iterates over None.
Was This Post Helpful? 0
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 745
  • Joined: 15-December 14

Re: Changing directories using a loop

Posted 28 June 2019 - 12:01 AM

Thanks guys for your advice I figured it out after reading your suggestions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1