Re: No os.copy()? Why not?

Discussion in 'Python' started by HoneyMonster, Apr 2, 2012.

  1. HoneyMonster

    HoneyMonster Guest

    On Wed, 28 Mar 2012 13:12:30 -0700, John Ladasky wrote:

    > I'm looking for a Python (2.7) equivalent to the Unix "cp" command.
    > Since the equivalents of "rm" and "mkdir" are in the os module, I
    > figured I look there. I haven't found anything in the documentation.
    > I am also looking through the Python source code in os.py and its child,
    > posixfile.py.
    >
    > Any help? Thanks.


    One way:
    import os

    os.system ("cp src sink")
     
    HoneyMonster, Apr 2, 2012
    #1
    1. Advertisements

  2. Am 02.04.2012 23:11 schrieb HoneyMonster:

    > One way:
    > import os
    >
    > os.system ("cp src sink")


    Yes. The worst way you could imagine.

    Why not the much much better

    from subprocess
    subprocess.call(['cp', 'src', 'sink'])

    ?

    Then you can call it with (really) arbitrary file names:


    def call_cp(from, to):
    from subprocess
    subprocess.call(['cp', '--', from, to])

    Try that with os.system() and from="That's my file"...


    Thomas
     
    Thomas Rachel, Apr 3, 2012
    #2
    1. Advertisements

  3. HoneyMonster

    John Ladasky Guest

    Re: No os.copy()? Why not?

    I use subprocess.call() for quite a few other things.

    I just figured that I should use the tidier modules whenever I can.
     
    John Ladasky, Apr 3, 2012
    #3
  4. Re: No os.copy()? Why not?

    Am 03.04.2012 11:34 schrieb John Ladasky:
    > I use subprocess.call() for quite a few other things.
    >
    > I just figured that I should use the tidier modules whenever I can.


    Of course. I only wanted to point out that os.system() is an even worse
    approach. shutils.copy() is by far better, of course.
     
    Thomas Rachel, Apr 4, 2012
    #4
    1. Advertisements

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. Steve Franks

    Copy Web tool does not copy sub dirs?

    Steve Franks, Sep 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    598
    Steve Franks
    Sep 15, 2005
  2. Alex
    Replies:
    2
    Views:
    1,610
  3. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    710
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,362
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,821
    Smokey Grindel
    Dec 2, 2006
  6. rdc02271
    Replies:
    24
    Views:
    1,023
    Jacek Dziedzic
    Dec 27, 2005
  7. Replies:
    26
    Views:
    2,573
    Roland Pibinger
    Sep 1, 2006
  8. Skybuck Flying
    Replies:
    16
    Views:
    926
    tragomaskhalos
    Aug 25, 2007
Loading...