Solved

Copy Files - Python

Posted on 2016-10-10
7
53 Views
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?
0
Comment
Question by:sj77
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Have a look at the built-in shutil module (stands for shell utilities).

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

Author Comment

by:sj77
Comment Utility
@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)
    print(filename)

Open in new window

0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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.

#!python3

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')):
    print(filename)
    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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Expert Comment

by:ltpitt
Comment Utility
If you provide us a better description of the final tool you plan to build maybe we can point you in the right direction
0
 

Author Comment

by:sj77
Comment Utility
@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?
0
 

Author Comment

by:sj77
Comment Utility
essentially i want to move and delete from source folder
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
Use the shutil.move() function instead of the copy() -- see https://docs.python.org/3.5/library/shutil.html#shutil.move
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 144
zeroMAx challenge 20 76
firstChar challenge 13 83
How can I do this in Pyhton? 12 66
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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now