Solved

Copy Files - Python

Posted on 2016-10-10
7
72 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
ID: 41837443
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
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)
    print(filename)

Open in new window

0
 
LVL 28

Expert Comment

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

#!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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:ltpitt
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
0
 

Author Comment

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

Author Comment

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

Accepted Solution

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now