Hi everyone im doing a little assignment in python and I need to do the following thing:
i have directories like /home/user/music, /home/user/photos and the like witch are inserted by the user and each new folder is a level so what i need to do is to create a method that recieves a number and then prints every folder that is at that level.
So far I have the creation of the list but i don’t know how i can count every single “/” in the strings that are added via keyboard and have them stored so i can know the levels and then compare them with the number the program recieves.
If anybody can give me at least some basic idea of where to start I will be very greatful. Thanks in advance
I want to do something like this (pseudo-code)
example of some strings:
/home/user/music
/home/user/photos
/home/user/research
/home/user/music/rock
n=raw_input
if n is equal to a specific amount of "/":
print "every string that has the same amount of "/"
FULL CODE SO FAR
print "Please add paths"
l1=raw_input("> ")
l2=raw_input("> ")
while True:
if l2 == "//":
break
else:
l1=l1+"\n"
l1=l1+l2
l2=raw_input("> ")
l1=l1.split("\n")
print "Your paths are"
print "\n"
print "\n" .join(l1)
print "\n"
print l1[1].count("/")
while True:
print "Choose an option "
print "1. Amount of sub folders of a path"
print "2. Amount of folders at a specified level"
print "3. Max depth between all the folders"
op=raw_input()
if op == "1":
print "Ingrese directorio"
d=raw_input()
if op == "2":
print "Ingrese nivel"
n=raw_input()
compararnivel(n)
raw_input()
If you are sure that the user enters the entire path name, and there are no relative paths etc., you can simply use str.count as follows:
If however you do need to manipulate the paths to first get them into the full path format, and/or do path validation, take a look at the functions provided in os.path
Ok, so here is some code explaining my comments:
You can add the raw_input data to a list instead of joining it to a string and splitting it up as follows:
The while loop can be better expressed as follows:
You should add an end condition to your 2nd while loop, perhaps by adding a 4th prompt asking the user to quit
Putting all this together, you get the following (Note my comments, they should help you)