#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Copy Files Using Python


    Dear All,

    I am looking for a very simple basic script to copy files from one folder to an other on my linux (suse 11.0) system

    i.e I have a file load.py in my /home/folder/ and I want to copy it to /home/folder2/

    with perl i can do is
    Code:
    system("cp /home/folder/load.py /home/folder2/");
    with now i wish to do the same basic task with a little python script

    Please guide me in this reagrds
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0
    cp isn't a perl command, its a command of the system you're on (perl just uses the system call to call one of these commands), so likewise python has something too that merely does the same thing:

    Code:
    import os
    os.popen('cp directory1/file directory2')
    Edit: Schol-R-REA's post is more correct, I just find that for some reason I started using popen for all this stuff (even though it returns a file object, as opposed to nothing, using system).
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    The exact equivalent of this in Python would be
    Code:
    import os
    
    os.system("cp /home/folder/load.py /home/folder2/")
    Or did you want to know how to write a Python program that does the actual copying itself?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Alternatively you can use the function in the shutil module:
    shutil.copytree(src, dst[, symlinks=False[, ignore=None]])

    Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories. Permissions and times of directories are copied with copystat(), individual files are copied using copy2().

    If symlinks is true, symbolic links in the source tree are represented as symbolic links in the new tree; if false or omitted, the contents of the linked files are copied to the new tree.

    If ignore is given, it must be a callable that will receive as its arguments the directory being visited by copytree(), and a list of its contents, as returned by os.listdir(). Since copytree() is called recursively, the ignore callable will be called once for each directory that is copied. The callable must return a sequence of directory and file names relative to the current directory (i.e. a subset of the items in its second argument); these names will then be ignored in the copy process. ignore_patterns() can be used to create such a callable that ignores names based on glob-style patterns.

    If exception(s) occur, an Error is raised with a list of reasons.

    The source code for this should be considered an example rather than the ultimate tool.

    Changed in version 2.3: Error is raised if any exceptions occur during copying, rather than printing a message.

    Changed in version 2.5: Create intermediate directories needed to create dst, rather than raising an error. Copy permissions and times of directories using copystat().

    Changed in version 2.6: Added the ignore argument to be able to influence what is being copied.
    Dave
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0
    Thanks a lot

    This code

    Code:
    import os  os.system("cp /home/folder/load.py /home/folder2/")
    solved my task

IMN logo majestic logo threadwatch logo seochat tools logo