Solved

Copy Files - Python

Posted on 2016-10-10
7
88 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 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - Save to file 4 68
allswap challenge 6 116
Work with App store 7 68
Problem to refer to value 8 62
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video teaches viewers about errors in exception handling.
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.

827 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