473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

shutil.copy in c

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 '07 #1
10 3173
yi*****@gmail.com 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
Jul 12 '07 #2
In article <11**********************@22g2000hsm.googlegroups. com>,
yi*****@gmail.com <yi*****@gmail.comwrote:
>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
Jul 12 '07 #3
Pietro Cerutti <ga**@gahr.chwrites:
yi*****@gmail.com 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) ks***@mib.org <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"
Jul 12 '07 #4
Pietro Cerutti <ga**@gahr.chwrote:
yi*****@gmail.com 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 ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 12 '07 #5
"yi*****@gmail.com" <yi*****@gmail.comwrote:
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
Jul 13 '07 #6
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"yi*****@gmail.com" <yi*****@gmail.comwrote:
>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) ks***@mib.org <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"
Jul 13 '07 #7
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
Jul 14 '07 #8
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"yi*****@gmail.com" <yi*****@gmail.comwrote:
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
Jul 16 '07 #9
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
[...]
>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) ks***@mib.org <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"
Jul 16 '07 #10
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
[...]
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
Jul 18 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ...
2
by: BartlebyScrivener | last post by:
I'm working on various backup scripts, using filecmp and shutil. When I run a script to copy files to a mapped network drive, shutil creates a backup file with a date of 2002 or so. If I use...
1
by: Russell Warren | last post by:
I just did a comparison of the copying speed of shutil.copy against the speed of a direct windows copy using os.system. I copied a file that was 1083 KB. I'm very interested to see that the...
6
by: Antoine De Groote | last post by:
Google tells quite some things about it, but none of them are satisfactory. I'm on Windows, and shutil operations (e.g. move, copy) throw Permission denied all the time, for the source files. It...
1
by: Hugo Ferreira | last post by:
Hi there, I have a problem. I'm using calling shutil.copyfile() followed by open(). The thing is that most of the times open() is called before the actual file is copied. I don't have this...
8
by: David Nicolson | last post by:
Hi, I wasn't exactly sure where to send this, I don't know if it is a bug in Python or not. This is rare, but it has occurred a few times and seems to be reproducible for those who experience...
10
by: Robert Dailey | last post by:
Hi, I'm trying to create a Python equivalent of the C++ "ifstream" class, with slight behavior changes. Basically, I want to have a "filestream" object that will allow you to overload the...
1
by: Ahmed, Shakir | last post by:
Need help to copy a personal geodatabase from one location to another: Trying to copy a personal geodatabase from one location to another location where all the users are retrieving data from...
4
by: klia | last post by:
hello folks i am trying to tweak the current codes so that later when i call it from the terminal i can provide sourcefile and the destination file rather being fixed in the code. because now i...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.