how to get os.system () call to cooperate on Windows

A

Anthra Norell

I have a Python program that needs to copy files around. I could read
and write which would be inefficient and would time-stamp the copy. The
module "os" has lots of operating system functions, but none that copies
files I could make out reading the doc twice. The function "os.system
('copy file_name directory_name')" turns out doesn't do anything except
flashing a DOS command window for half a second. So my question is: How
can one copy files on the OS level?
This simple question would surely have been asked many times before.
Not to impose on anyone's patience I expect it can be answered between
two sips of coffee.

Thanks very much

Frederic
 
D

Diez B. Roggisch

Anthra said:
I have a Python program that needs to copy files around. I could read
and write which would be inefficient and would time-stamp the copy. The
module "os" has lots of operating system functions, but none that copies
files I could make out reading the doc twice. The function "os.system
('copy file_name directory_name')" turns out doesn't do anything except
flashing a DOS command window for half a second. So my question is: How
can one copy files on the OS level?
This simple question would surely have been asked many times before.
Not to impose on anyone's patience I expect it can be answered between
two sips of coffee.

Thanks very much

Did you take a look at the shutil-module?

Diez
 
A

Anthra Norell

Diez said:
Anthra Norell wrote:



Did you take a look at the shutil-module?

Diez
No, I didn't. There's a number of modules I know by name only and
shutils was one of them. A quick peek confirmed that it is exactly what
I am looking for. Thank you very much for the advice.

Frederic
 
T

TerryP

The function "os.system
('copy file_name directory_name')" turns out doesn't do anything except
flashing a DOS command window for half a second. So my question is: How
can one copy files on the OS level?

Under a Windows system the built in command, copy, is a pile of crap
and xcopy is not much fun; you need to screw with it at the command
prompt to find the exact usage.

The formal way to copy files on the 'OS level' is by way of a system
call. I believe under Windows NT, this would be the CopyFile family;
using that through cctypes doesn't sound like fun.
 
G

Gabriel Genellina

Under a Windows system the built in command, copy, is a pile of crap
and xcopy is not much fun; you need to screw with it at the command
prompt to find the exact usage.

Uh... well, not because you don't know the command syntax it becomes a
pile of crap...
The formal way to copy files on the 'OS level' is by way of a system
call. I believe under Windows NT, this would be the CopyFile family;
using that through cctypes doesn't sound like fun.

It's as simple as this:

from ctypes import windll
CopyFile = windll.kernel32.CopyFileA
CopyFile("d:\\temp\\old.txt", "d:\\temp\\new.txt", True)
 
S

Steven D'Aprano

[...]

It's as simple as this:

from ctypes import windll
CopyFile = windll.kernel32.CopyFileA
CopyFile("d:\\temp\\old.txt", "d:\\temp\\new.txt", True)



Have I missed something? What's wrong with shutil?

shutil.copyfile(source, destination) seems to work for me, even on
Windows.
 
G

Gabriel Genellina

En Tue, 27 Oct 2009 23:04:47 -0300, Steven D'Aprano
Have I missed something? What's wrong with shutil?

There's nothing wrong with shutil, but CopyFile and friends have some
additional features (correct handling of alternate data streams, progress
callback, reduced disk fragmentation...) that some people may be
interested in.

The idea was just to show how to call CopyFile using ctypes, not implying
that it's the only way to do that. Everyone knows that the One and True
Way of copying files is using PIP.
 
D

David Robinow

The idea was just to show how to call CopyFile using ctypes, not implying
that it's the only way to do that. Everyone knows that the One and True Way
of copying files is using PIP.
Yuk! With the "/OP" syntax. Which leads to the need for a backslash.
Bill Gates learned on the PDP-11 and it's still plaguing us today.

Anyway, thanks for the example. I'll add it to my useful Windows snippets.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top