file backup in windows

John Machin sjmachin at lexicon.net
Wed Nov 22 05:03:00 EST 2006


k.i.n.g. wrote:
> Hi ALL,
>
> I am a newbee programmer and started of with python recently. I am
> trying write a script which backups outlook (.pst ) files everytime I
> shutdown my system. I have writtern following code after some findings
> on net. My .pst file path is as follows
>
> " c:\documents and settings\060577\Local Settings\Application
> Data\Microsoft\Outlook "
> where 060577 represents username. I want my script to identigy the user
> logged in and go to the resp outlook folder or should be able to read
> outlook store directory path from registry and the copy the files to
> the desired path.
>
> ---------------------------------------------------
> how can i make the following code work, I have probelm with filepath
> declaration.
> ---------------------------------------------------
> import os, shutil
> filepath = ' C:\\Documents and Settings\\060577\\Local
> Settings\\Application Data\\Microsoft\\Outlook\\* '
> backup = ' D:\\temp\\outlook '

Aside: having a space at the beginning and/or end of the filename has
no good effect and may cause problems, so don't do it.

> os.system ("xcopy /s %s %s" % (filepath, backup))
> -----------------------------------------

It's always a good idea *before* you write an os.system call on *any*
operating system to try a few sample commands at the command line. You
would find in this case that the problem exists there too -- it has
nothing to do with Python. The problem is that the first argument
*contains* spaces, but the Windows command processor splits the command
line on spaces, so it thinks the first argument is  'C:\\Documents'. On
both the command line and in your script, you will need to wrap quotes
around each argument that does/could contain spaces.

[untested]
os.system ('xcopy /s "%s" "%s"' % (filepath, backup))

Hint: you should find it easier using raw strings for Windows
filenames:
    backup = r'D:\temp\outlook'

HTH,
John




More information about the Python-list mailing list