Python Tutorial

Wednesday, May 15, 2013

Shutil: high level file operations

The shutil module provides a number of high-level operations on files and collections of files.

import shutil
import os

# copy(& rename) file from src to dst.
src = os.getcwd()+"\\"+"a.txt"
dst = os.getcwd()+"\\"+"data\\new_a.txt"
shutil.copyfile(src, dst)

# copy file from src to dst directory.
src = os.getcwd()+"\\"+"a.txt"
dst = os.getcwd()+"\\data2"
shutil.copy(src, dst)

    Recursively copy an entire directory tree rooted at src.
    dst file must not exists 
src = os.getcwd()+"\\" + "data"
dst = os.getcwd()+"\\" + "data3"
shutil.copytree(src, dst)

# Recursively move a file or directory to another location.
src = os.getcwd()+"\\" + "data3"
dst = os.getcwd()+"\\" + "data4"
shutil.move(src, dst)

# delete entire directory
dest_file = os.getcwd()+"\\data3"


Post a Comment