Searching files in directories

Discussion in 'Python' started by pkilambi@gmail.com, Oct 14, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Larry Bates Guest

    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

    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
    >
     
    Larry Bates, Oct 14, 2005
    #2
    1. Advertising

  3. "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
     
    George Sakkis, Oct 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    494
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    533
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    830
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Paul F
    Replies:
    4
    Views:
    638
    Mark Fitzpatrick
    Jun 25, 2008
  5. Adam Petrie
    Replies:
    8
    Views:
    309
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page