Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Searching files in directories

Reply
Thread Tools

Searching files in directories

 
 
pkilambi@gmail.com
Guest
Posts: n/a
 
      10-14-2005
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

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      10-14-2005
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

http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>

 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      10-14-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual directories/physical directories in IIS Paul F ASP .Net 4 06-25-2008 04:04 PM
Multiple bin-directories with virtual directories? =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?= ASP .Net 0 11-09-2004 05:49 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
How to map Project directories to Production sub-directories Joel Finkel ASP .Net 0 09-12-2003 06:47 PM
Using virtual directories for common directories (scripts, images, styles, etc.) Jeffry van de Vuurst ASP .Net 2 07-30-2003 07:00 PM



Advertisments