Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:10:06+00:00 2026-06-04T12:10:06+00:00

Hi everyone im doing a little assignment in python and I need to do

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:10:08+00:00Added an answer on June 4, 2026 at 12:10 pm

    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:

    >>> str.count('/home/usr/music', '/')
    3
    

    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:

    1. You can add the raw_input data to a list instead of joining it to a string and splitting it up as follows:

      l1 = [] # This makes an empty list
      l2 = raw_input("> ") # Do this statement in a loop if you like
      l1.append(l2) # This appends l2 to the list
      
    2. The while loop can be better expressed as follows:

      while l2!='//':
        append l2 to the list
        prompt the user for more input
      
    3. 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)

    print "Please add paths, enter // to stop" # It is a good idea to tell the user how to stop the input process
    
    l1 = [] # Create empty list
    l2 = raw_input('> ') # Ideally, you should verify that the user entered a valid path (eg: are '/' allowed at the end of a path?)
    
    while l2 != '//':
        l1.append(l2)
        l2 = raw_input('> ')
    
    print "Your paths are: "
    for path in l1:
        print path
    
    # I am not sure what you are trying to achieve with "print l1[1].count("/")" so leaving it out
    
    # I would suggest to print the options outside the loop, it's a matter of taste, 
    # but do you really want to keep printing them out?
    print "Choose an option: \n\
           1. Number of subfolders of path\n\
           2. Number of folders at a specific path\n\
           3. Max depth between all folders\n\
           4. Quit\n"
    
    choice = raw_input('Enter choice: ')
    
    while choice!='4':
        if choice == '1':
            path = raw_input('Which path are you looking for?')
                # Calculate and print the output here
    
        # This is the question you asked, so I am answering how to process this step    
        if choice == '2':
            level = raw_input('Which level? ') # Ideally, you should verify that the user indeed enters an int here
    
            # Since your python does not seem to be very advanced, let's use a simple for loop:
            num=0
            for path in l1:
                if str.count(path, '/')==int(level):
                    num = num+1
    
            print 'Number of such paths is: ', num
    
        if choice == '3':
            print 'this is not yet implemented'
            # Do something
    
        choice = raw_input('Enter choice: ')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little bit of code that looks just like this: function StrippedExample(i1,
EveryOne I am doing xml parsing like This public class XMLHandler extends DefaultHandler{ //
i know everyone says to avoid doing something like this because its very slow
I think it's safe to say everyone loves doing something like this in Rails:
Good evening everyone, I have a quick question on a homework assignment my class
Hey everyone, great community you got here. I'm an Electrical Engineer doing some programming
HTTP Referer is the way I'm doing it at the moment. As everyone who's
everyone seems interested in building IPhone apps today. Do you have to have an
everyone. Please see example below. I'd like to supply a string to 'schedule_action' method
That may sound a little confusing. Basically, I have a function CCard newCard() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.