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 8075965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:09:53+00:00 2026-06-05T15:09:53+00:00

I am trying to build a simple script which must be able to navigate

  • 0

I am trying to build a simple script which must be able to navigate through directories, sub-directories, and sub-sub-directories, and I have never done it before.

Why does this code generate the output that it does–with the double-backslash? Also, what should my code be doing, given my objectives?

I believe my objective is to maintain the single backslash, but I am unsure. All I know is I will be bouncing between different levels within the same directory–into sub-directories and sub-sub-directories. Trying to work through the program further, I believe I am running into errors by trying to look for files with filepaths that contain double backslashes.

import os, shutil, time

mdir = 'C:\\Users\Dev\Desktop\Python_Test'
dirlist = [(mdir + '\\' + i) for i in os.listdir(mdir) if os.path.isdir(os.path.join(mdir, i))]

print dirlist

Output:

['C:\\Users\\Dev\\Desktop\\Python_Test\\Dir 1',
 'C:\\Users\\Dev\\Desktop\\Python_Test\\Dir 2']
  • 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-05T15:09:54+00:00Added an answer on June 5, 2026 at 3:09 pm

    The double backslash is there because you’re calling print on a list, which prints the repr of the list’s elements:

    >>> l = ['C:\\Users\\Dev\\Desktop\\Python_Test\\Dir 1',
    ...      'C:\\Users\\Dev\\Desktop\\Python_Test\\Dir 2']
    >>> for x in l:
    ...     print(repr(x))
    ...     
    'C:\\Users\\Dev\\Desktop\\Python_Test\\Dir 1'
    'C:\\Users\\Dev\\Desktop\\Python_Test\\Dir 2'
    

    repr is also giving you the quotes around the strings. The idea is that the output of repr (for simple types like str) can be fed back to the Python interpreter directly. To print without quotes and with single backslashes, print the elements separately:

    >>> for x in l:
    ...     print(x)
    ...     
    C:\Users\Dev\Desktop\Python_Test\Dir 1
    C:\Users\Dev\Desktop\Python_Test\Dir 2
    

    Use this form when communicating with the outside world.

    Btw., for walking directory hierarchies, use os.walk and for constructing paths, use os.path.join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple image rotator script that I'm trying to build but I
I'm trying to build a simple environment check script for my firm's test environment.
I'm trying to build the simple layout that I have got in the image
I'm trying to build a simple deployment script for my PHP apps. I know
I am trying to build a very simple price comparison script. Until now, I
I'm trying to build a simple component extending spark.components.Group to have a background color,
I'm trying to build a very simple VPS control panel; i have the following
I'm trying to build a simple Magento Module that needs to connect to the
I am trying to build a simple booking application for a school presented as
I am trying to build a simple nested html menu using HAML and am

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.