Copy Files - Python

Posted on 2016-10-10
Last Modified: 2016-10-12
Hello Experts-

I am trying to copy a files in one directory source to destination directory.

ex: copy and overwrite all files in c:\source to c:\destination

What is the easiest way to do this?
Question by:sj77
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 29

Expert Comment

ID: 41837443
Have a look at the built-in shutil module (stands for shell utilities).

You will find copy() and copy2(), and copytree().

Author Comment

ID: 41837444
@pepr I did this and it worked.

Do you think it would be best to wild card for *.mdb or just leave the code as is?

import os
import shutil
os.chdir('C:\\') #Make sure you add your source and destination path below

dir_src = ("C:\\source\\")
dir_dst = ("C:\\destination\\")

for filename in os.listdir(dir_src):
    if filename.endswith('.mdb'):
        shutil.copy( dir_src + filename, dir_dst)

Open in new window

LVL 29

Expert Comment

ID: 41837491
It depends. Firstly, the line 3 does nothing for the purpose. The lines 5 and 6 should not use parentheses, and probably should use single quotes (for consistency). The glob module allows you to work with the mask directly. Anyway, I recommend to get used to os.path.join() function. It solves all problems with missing/extra (back)slashes in the path.

Get used to normal slashes. One day, you may want to move your code to UNIX-based OS, and it will be easier. You get nothing when using backslashes. For print, use os.path.normpath() if you are picky.

If you want to copy also the last modification time... use copy2().

Print filename usually before the operation. When it crashes, you will know for what file.


import glob
import os
import shutil

dir_src = 'C:/source'
dir_dst = 'C:/destination'

for filename in glob.iglob(os.path.join(dir_src, '*.mdb')):
    shutil.copy(filename, dir_dst)

Open in new window

If you need to construct both paths (source and destination), then probably os.listdir() with os.path.join() is better.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 41839984
If you provide us a better description of the final tool you plan to build maybe we can point you in the right direction

Author Comment

ID: 41840585
@pepr if I want to move files in a directory to a different destination after this copy has been done how would this be done?

example: c:\source and c:\dest

copy gets done first > program action > then move files in c:\dest to c:\dest_final?

Author Comment

ID: 41840640
essentially i want to move and delete from source folder
LVL 29

Accepted Solution

pepr earned 500 total points
ID: 41840907
Use the shutil.move() function instead of the copy() -- see

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to split this type of line? 5 115
windows 10 bash shell 4 52
Path to Python 9 144
convert Systemjs to Webpack 3 124
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question