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. Advertising

  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. Advertising

  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. 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. Alex
    Replies:
    2
    Views:
    1,224
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    880
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,849
    Smokey Grindel
    Dec 2, 2006
  4. rdc02271
    Replies:
    24
    Views:
    737
    Jacek Dziedzic
    Dec 27, 2005
  5. Replies:
    26
    Views:
    2,113
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page