Searching files in directories

George Sakkis gsakkis at rutgers.edu
Fri Oct 14 14:32:38 EDT 2005


"Larry Bates" wrote:

> Not tested but should be close:
>
> import os
> import shutil
>
> files = ['file1.txt']
> source_directory      = '/tmp/source/'
> destination_directory = '/tmp/destination/'
>
> for file in files:
>     src=os.path.join(source_directory, file
>     dst=os.path.join(destination_directory, file
>     if os.path.exists(os.path.join(source_directory, file):
>         shutil.copy(src, dst)


Or more succinctly using the path module:

from path import path

files = ['file1.txt']
src_dir = path('/tmp/source/')
dest_dir = path('/tmp/destination/')

for filename in files:
    srcfile = src_dir / filename
    if srcfile.exists():
        srcfile.copy(dest_dir)


George




More information about the Python-list mailing list