Solved

Copy Files - Python

Posted on 2016-10-10
7
155 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
[X]
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
7 Comments
 
LVL 29

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 29

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
Independent Software Vendors: 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!

 
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 29

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

Technology Partners: 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!

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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