Home > Python > Python Coping Files to Another Folder

Python Coping Files to Another Folder

A simple python for copying files from one place to another.


#!/usr/bin/env python
""" Copy files (from one dir) to another dir
Note: if same name of file exist, it will over-write it !!!

Example:

python pTransFer.py m /home/vanessa_wdi/Desktop /home/vanessa_wdi/Downloads/test

"""
import glob, os
import shutil

def oneFile_Trans(src_file, des_dir):
    shutil.copy(src_file, des_dir)

def multiFiles_Trans(src_dir, des_dir):
    for name in glob.glob(src_dir+ os.sep + '*'):
        print name
        if os.path.isfile(name):
            print("Transfer file", name)
            shutil.copy(name, des_dir)

def main():
    import sys
    mode = sys.argv[1]
    src = sys.argv[2]
    des = sys.argv[3]
    if mode == "s":  # Single file trans
        oneFile_Trans(src, des)
    elif mode == "m":  # multiple file trans
        multiFiles_Trans(src, des)

def test_example():
    """ Test Examples"""
    src_file = r"/home/vanessa_wdi/Desktop/FredlundR.pdf"
    des_dir = r"/home/vanessa_wdi/Downloads/test"
    src_dir = r"/home/vanessa_wdi/Downloads/nmfplsa"

    oneFile_Trans(src_file, des_dir)
    multiFiles_Trans(src_dir, des_dir)

if __name__=="__main__":
    main()
    #test_example()

Advertisements
Categories: Python Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: