shutil.copy in c

Discussion in 'C Programming' started by yinglcs@gmail.com, Jul 12, 2007.

  1. Guest

    Hi,

    Is there a c library which does shutil.copy2() in python? Basically
    copy a file from 1 directory to another?

    import shutil
    import os

    shutil.copy2(r"C:\test\test",r"C:\test1\test")

    Thank you.
     
    , Jul 12, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > Is there a c library which does shutil.copy2() in python? Basically
    > copy a file from 1 directory to another?


    Standard C doesn't know anything about files or directories.
    What you need is an OS-specific library function / system call, better
    discussed in a newsgroup related to your system.

    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Jul 12, 2007
    #2
    1. Advertising

  3. In article <>,
    <> wrote:

    >Is there a c library which does shutil.copy2() in python? Basically
    >copy a file from 1 directory to another?


    Not in standard C, no. There is no file copy utility in the
    C standard.

    There will likely be the usual responses about opening the file,
    reading it a char at a time and writing the chars to an output
    file. However, "files" may have attributes other than just
    their contents (especially if the filename is a device!),
    and there is no way in standard C to discover or duplicate
    those attributes. I would suspect that python's shutil.copy2()
    *does* have a way of detecting and dealing with those attributes.

    In Windows (based upon the filenames you gave), the sort
    of attributes you might want copied could include the
    alternate data streams, and the security rights.
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
     
    Walter Roberson, Jul 12, 2007
    #3
  4. Pietro Cerutti <> writes:
    > wrote:
    >> Is there a c library which does shutil.copy2() in python? Basically
    >> copy a file from 1 directory to another?

    >
    > Standard C doesn't know anything about files or directories.
    > What you need is an OS-specific library function / system call, better
    > discussed in a newsgroup related to your system.


    It doesn't know about directories, but it does know a thing or two
    about files.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 12, 2007
    #4
  5. Pietro Cerutti <> wrote:
    > wrote:
    > > Is there a c library which does shutil.copy2() in python? Basically
    > > copy a file from 1 directory to another?


    > Standard C doesn't know anything about files or directories.


    Nitpick alarm: the C standard luckily at least admits to the
    existence of files, otherwise functions like fopen(), fread()
    etc. would rather likely not exist (or do something else than
    what they are required to do;-)
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jul 13, 2007
    #5
  6. Richard Bos Guest

    "" <> wrote:

    > Is there a c library which does shutil.copy2() in python? Basically
    > copy a file from 1 directory to another?
    >
    > import shutil
    > import os
    >
    > shutil.copy2(r"C:\test\test",r"C:\test1\test")


    I cannot fathom the need for a specific function to copy from one
    directory to another, given both complete file names. Surely if you have
    filename1 and filename2, that should allow you to do a copy from the one
    to the other regardless of whether either of them is in any directory or
    whether the system has directories at all?
    In any case, given two file names, the procedure in C is simple.
    fopen(filename1, "rb"), fopen(filename2, "wb"), fread() from file 1 and
    fwrite() to file 2 until you run out of data, fclose() both files.
    That's it. No need to fret about directories.

    Richard
     
    Richard Bos, Jul 13, 2007
    #6
  7. (Richard Bos) writes:
    > "" <> wrote:
    >> Is there a c library which does shutil.copy2() in python? Basically
    >> copy a file from 1 directory to another?
    >>
    >> import shutil
    >> import os
    >>
    >> shutil.copy2(r"C:\test\test",r"C:\test1\test")

    >
    > I cannot fathom the need for a specific function to copy from one
    > directory to another, given both complete file names. Surely if you have
    > filename1 and filename2, that should allow you to do a copy from the one
    > to the other regardless of whether either of them is in any directory or
    > whether the system has directories at all?
    > In any case, given two file names, the procedure in C is simple.
    > fopen(filename1, "rb"), fopen(filename2, "wb"), fread() from file 1 and
    > fwrite() to file 2 until you run out of data, fclose() both files.
    > That's it. No need to fret about directories.


    If you need your program to be as portable as possible, that's the way
    to do it. But very often there can be requirements other than
    portability. If I had a need to copy a file in a C program intended
    to run under a Unix-like system, I'd invoke the "cp" command; this is
    likely (but not certain) to be faster than a pure C solution, and it
    gives me more options regarding *how* to copy the file (most of which
    are outside the scope of C). Under Windows (implied by the file names
    used by the OP), I suppose I'd use "copy" for the same reasons.

    I'm not very familiar with Python, but I presume that 'shutil.copy2'
    is an abstraction that invokes "cp", or "copy", or whatever. With
    some effort, you can build the same kind of abstraction in C -- and
    I'm sure it's already been done.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 13, 2007
    #7
  8. On Fri, 13 Jul 2007 10:58:13 GMT, Richard Bos wrote:
    >In any case, given two file names, the procedure in C is simple.
    >fopen(filename1, "rb"), fopen(filename2, "wb"), fread() from file 1 and
    >fwrite() to file 2 until you run out of data, fclose() both files.
    >That's it.


    A production quality copy function in C is not trivial. The code you
    can find on the internet is mostly defective. A production quality
    copy function at least needs:
    - an explicit policy wrt overwriting an existing file
    - preparation for the worse cases, ie. return values of read, write,
    and close functions and/or stream state must be checked
    - preparation for the worst case, the crash during copying

    The last requirement implies that the file is first written to a
    temporary file (in the same directory as the destination file) and
    renamed later after all copying is reliably finished. A production
    quality copy function would probably use the Posix I/O functions
    instead of stdio because they provide some essential features not
    present in stdio, e.g. you can open a file with O_CREAT | O_EXCL or
    flush data to disk with fsync (_commit on Windows).


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
     
    Roland Pibinger, Jul 14, 2007
    #8
  9. Richard Bos Guest

    Keith Thompson <> wrote:

    > (Richard Bos) writes:
    > > "" <> wrote:
    > >> Is there a c library which does shutil.copy2() in python? Basically
    > >> copy a file from 1 directory to another?
    > >>
    > >> import shutil
    > >> import os
    > >>
    > >> shutil.copy2(r"C:\test\test",r"C:\test1\test")

    > >
    > > I cannot fathom the need for a specific function to copy from one
    > > directory to another, given both complete file names. Surely if you have
    > > filename1 and filename2, that should allow you to do a copy from the one
    > > to the other regardless of whether either of them is in any directory or
    > > whether the system has directories at all?
    > > In any case, given two file names, the procedure in C is simple.
    > > fopen(filename1, "rb"), fopen(filename2, "wb"), fread() from file 1 and
    > > fwrite() to file 2 until you run out of data, fclose() both files.
    > > That's it. No need to fret about directories.

    >
    > If you need your program to be as portable as possible, that's the way
    > to do it. But very often there can be requirements other than
    > portability.


    True enough, though I doubt it's going to make much of a difference in
    speed at least unless you copy a lot of files.

    > If I had a need to copy a file in a C program intended
    > to run under a Unix-like system, I'd invoke the "cp" command; this is
    > likely (but not certain) to be faster than a pure C solution, and it
    > gives me more options regarding *how* to copy the file (most of which
    > are outside the scope of C). Under Windows (implied by the file names
    > used by the OP), I suppose I'd use "copy" for the same reasons.


    I would hesitate to call the actual program, presumably using system()
    or something similar but system-specific. Most APIs should have
    functions to do all that for you.

    Richard
     
    Richard Bos, Jul 16, 2007
    #9
  10. (Richard Bos) writes:
    > Keith Thompson <> wrote:

    [...]
    >> If I had a need to copy a file in a C program intended
    >> to run under a Unix-like system, I'd invoke the "cp" command; this is
    >> likely (but not certain) to be faster than a pure C solution, and it
    >> gives me more options regarding *how* to copy the file (most of which
    >> are outside the scope of C). Under Windows (implied by the file names
    >> used by the OP), I suppose I'd use "copy" for the same reasons.

    >
    > I would hesitate to call the actual program, presumably using system()
    > or something similar but system-specific. Most APIs should have
    > functions to do all that for you.


    Really? The API I'm most familiar with (Unix) doesn't provide a
    function to copy an entire file, as far as I know, other than invoking
    the 'cp' program.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 16, 2007
    #10
  11. Richard Bos Guest

    Keith Thompson <> wrote:

    > (Richard Bos) writes:
    > > Keith Thompson <> wrote:

    > [...]
    > >> If I had a need to copy a file in a C program intended
    > >> to run under a Unix-like system, I'd invoke the "cp" command; this is
    > >> likely (but not certain) to be faster than a pure C solution, and it
    > >> gives me more options regarding *how* to copy the file (most of which
    > >> are outside the scope of C). Under Windows (implied by the file names
    > >> used by the OP), I suppose I'd use "copy" for the same reasons.

    > >
    > > I would hesitate to call the actual program, presumably using system()
    > > or something similar but system-specific. Most APIs should have
    > > functions to do all that for you.

    >
    > Really? The API I'm most familiar with (Unix) doesn't provide a
    > function to copy an entire file, as far as I know, other than invoking
    > the 'cp' program.


    It doesn't? Odd. I can't imagine where I got that impression, then.
    Except, of course, that MS Windows does have one.

    Richard
     
    Richard Bos, Jul 18, 2007
    #11
    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. Joe

    file copy shutil

    Joe, Feb 9, 2004, in forum: Python
    Replies:
    1
    Views:
    336
    Michael Hudson
    Feb 9, 2004
  2. Russell Warren
    Replies:
    1
    Views:
    790
    Tim Roberts
    Apr 27, 2006
  3. Tim Golden
    Replies:
    0
    Views:
    607
    Tim Golden
    Apr 27, 2006
  4. dave
    Replies:
    6
    Views:
    1,036
  5. Fletcher Johnson

    Why is the shutil module called shutil?

    Fletcher Johnson, Sep 24, 2011, in forum: Python
    Replies:
    11
    Views:
    878
    alex23
    Sep 26, 2011
Loading...

Share This Page