473,581 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3205
yi*****@gmail.c om 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************ **********@22g2 000hsm.googlegr oups.com>,
yi*****@gmail.c om <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.chwr ites:
yi*****@gmail.c om 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_Keit h) 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.chwr ote:
yi*****@gmail.c om 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_Keit h) 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(filename 1, "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.orgw rote:
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.orgw rote:
[...]
>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_Keit h) 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

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

Similar topics

1
7745
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 ---------------------------------------------------------------------------- ----------------- import re, os, time, shutil os.chdir(os.environ+os.environ+"\\Desktop") DESKTOP =...
2
2099
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 shutil.copy2 it copies the dates of the original files, but creates folders with similary old dates and times. Anybody ever seen that before? My...
1
3446
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 shutil.copy copyfileobj implementation of hacking through the file and writing a new one is significantly faster... any clue as to why this is? I...
6
14544
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 seems that this is the case for all my files. But what I don't understand is that yesterday it still worked. I didn't change anything on my system...
1
6107
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 problem when doing a step-by-step debug, since I give enough time for the OS to copy the file, but at run-time, it throws an exception. Is there...
8
7603
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 it. Examine this code: This is the output: exceptions.UnicodeDecodeError
10
3320
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 '<<' and '>>' operators to stream out and stream in data, respectively. So far this is what I have: class filestream:
1
2747
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 the second location: 1. I can copy over the updated personal geodatabase to the working location and overwrite it, though the file is opened by...
4
2600
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 have to specify the sourcefile and the destinationfile in codes and not left to be specified from the terminal. i want to be able to do this. ...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.