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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:00:23+00:00 2026-06-01T19:00:23+00:00

I write a function to copy files from directory A to directory B recursive.

  • 0

I write a function to copy files from directory A to directory B recursive.
The code is like this:

import os
import shutil
import sys
from os.path import join, exists

def copy_file(src, dest):
    for path, dirs, files in os.walk(src, topdown=True):
        if len(dirs) > 0:
            for di in dirs:
                copy_file(join(path, di), join(dest,  di))

        if not exists(dest):
            os.makedirs(dest)
        for fi in files:
            shutil.copy(join(path, fi), dest)

In my test, the input args are like this:

src = d:/dev

and it have one sub directory named py. Also, py has a sub directory named test

dest = d:/dev_bak

So, when i test my code, something strange happened.
In my dest directory which is d:/dev_bak, three sub directories are created.
That is: d:/dev_bak/py; d:/dev_bak/py/test; d:/dev_bak/test.

In my design, the structure of dev_bak will be same as dev. So, why this happened!

  • 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-01T19:00:24+00:00Added an answer on June 1, 2026 at 7:00 pm

    You can easily diagnose this by putting

        print path, dirs, files
    

    right below

    for path, dirs, files in os.walk(src, topdown=True):
    

    Essentially, you’re recursing twice.

    By itself, os.walk descends into subdirectories. You’re double-descending by recursively calling your own function. Here is some example output from that print statement:

    >>> copy_file("c:\Intel", "c:\Intel-Bak")
    c:\Intel ['ExtremeGraphics', 'Logs'] []
    c:\Intel\ExtremeGraphics ['CUI'] []
    c:\Intel\ExtremeGraphics\CUI ['Resource'] []
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\ExtremeGraphics\CUI ['Resource'] []
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\Logs [] ['IntelChipset.log', 'IntelControlCenter.log', 'IntelGFX.log', 'IntelGFXCoin.log']
    c:\Intel\ExtremeGraphics ['CUI'] []
    c:\Intel\ExtremeGraphics\CUI ['Resource'] []
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\ExtremeGraphics\CUI ['Resource'] []
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\ExtremeGraphics\CUI\Resource [] ['Intel\xae Graphics and Media Control Panel.lnk', 'Intel\xae HD Graphics.lnk']
    c:\Intel\Logs [] ['IntelChipset.log', 'IntelControlCenter.log', 'IntelGFX.log', 'IntelGFXCoin.log']
    

    As you can see, the directories get visited twice.

    You should fix the logic of your program so it visits each directory only once, but theoretically you could just ignore any directory you’ve already been to:

    visited = []
    def copy_file(src, dest):
        for path, dirs, files in os.walk(src, topdown=True):
            if path not in visited:
                for di in dirs:
                    print dest, di
                    copy_file(join(path, di), join(dest,  di))
                if not exists(dest):
                    os.makedirs(dest)
                for fi in files:
                    shutil.copy(join(path, fi), dest)
                visited.append(path)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I write this function, but what's its return value. (defn read-data [file] (let [code
I would like to write a little code that copy on a local pc
What would be the best way to write a generic copy constructor function for
I'm trying to write a jquery funciton that will copy specified fields from the
I wrote the following function to split the given full path into directory, filename
Suppose I write my code very defensively and always check the return types from
I have got this JavaScript code for uploading files to my server (named it
I'm trying to create a function to read Morse code from one file, convert
So(just for fun), i was just trying to write a C code to copy
I'm trying to write function to do the number_format() job for the non-ascii numbers

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.