By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Searching files in directories

P: n/a
can anyone help me with this...

I want to search for a list for files in a given directory and if it
exists copy them to destination directory

so what i am looking for is :

file = 'file1.txt'
source_directory = '/tmp/source/'
destination_directory = '/tmp/destination/'

so If the file exists in source_directory cp that file to the
destination_directory..

hope I am clear

Oct 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)

-Larry Bates

pk******@gmail.com wrote:
can anyone help me with this...

I want to search for a list for files in a given directory and if it
exists copy them to destination directory

so what i am looking for is :

file = 'file1.txt'
source_directory = '/tmp/source/'
destination_directory = '/tmp/destination/'

so If the file exists in source_directory cp that file to the
destination_directory..

hope I am clear

Oct 14 '05 #2

P: n/a
"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

Oct 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.