[Tutor] Batch file copy script

Luke Paireepinart rabidpoobear at gmail.com
Mon Aug 6 10:10:53 CEST 2007


Majid wrote:
> Hi all,
> I am new to Python and this mailing list. I wondered if someone would be 
> kind enough to give me a basic python skeleton to achieve the following.
>
> Many thanks in advance
> Majid
>
> ---------------------------------
>
>
>       Here are the assumptions:
>
> 1. we have a list.txt file with entries like:
>
>         /dists/dapper/Release.gpg
>         /dists/dapper/main/binary-i386/Release
>         /dists/dapper/restricted/binary-i386/Release
>         /doc/install/manual/example-preseed.txt.gz
>         /doc/install/manual/en/apa.html
>         /doc/install/manual/en/apas01.html
>
>     2. The files referenced in the list file (and many other files) are
>     in C:\source\ directory and subdirectories as defined in the list
>     file - e.g. for the first entry above we have this file:
>
>         C:\source\dists\dapper\Release.gpg
>
>     3. We want only the files listed in the list file to be copied to
>     D:\target\ keeping the same directory structure. So for the first
>     entry we want to have this file:
>
>         D:\target\dists\dapper\Release.gpg
>
>     4. If a file listed in the list file is not found in the source, we
>     want an entry to be added to C:\py\missing.txt (by the way the
>     python script doing the job could be in this directory too - i.e.
>     C:py\packagemaker.py - also the list file is here too: C:\py\list.txt)
>
>
>       Here is a possible logic for the script:
>
>     1. Open the list (C:\py\list.txt).
>     2. Until the end of the file
>        3. Read a line from list.txt (e.g. '/dists/dapper/Release.gpg')
>        4. Change forward slashes to back slashes (e.g.
>     '\dists\dapper\Release.gpg')
>        5. Add 'C:\source' to the string  (e.g.
>     'C:\source\dists\dapper\Release.gpg')
>        6. If the file exists in the source folder
>           7. Copy the file from the source frolder to the target folder
>     (e.g. copy C:\source\dists\dapper\Release.gpg to
>     D:\target\dists\dapper\Release.gpg)
>        8. Else
>           9. Add the name of the missing file to missings.txt
>     10. End of loop
>   
import os, shutil
target_dir = r'D:\target\'
source_dir = r'C:\source\'
missing_files = open("missings.txt","w")
for line in open('list.txt'):
    source_file = os.path.normpath(source_dir + line)
    target_file = os.path.normpath(target_dir + line)  
    if os.path.exists(target_file):
       shutil.copyfile(source_file, target_file)
    else:
        missing_files.write(line)
missing_files.close()
      
It's pretty straight-forward.
Is anything in there confusing?
Things that you might not have seen before:
the r in front of the string makes it a 'raw string' meaning that a \t 
is a "\" and then a "t" instead of a tab character, and this goes for 
all other \s.
In other words, the '\' isn't used to escape certain special ascii 
sequences, it's just a '\'.

Secondly, the os.path.normpath() function will fix your path for your OS.
SO if you have
C:/some/path/to/file.ext
it will switch it to
C:\\some\\path\\to\\file.ext
if you're on Windows.
A good feature of this function is that it also fixes multiple slashes, so
C:\\source\\/dapper
will be correctly changed to
C:\\source\\dapper

Pretty cool function that I didn't know about 'til now.
>
>       Possible uses of the script?
>   
Not sure, you could probably generalize it quite a bit more than I did, 
if you found other use cases.
> I have copied the contents of an ubuntu dvd to my hard drive 
> (C:\source\). It contains many files and directories totaling 3.45 GB. I 
> have also downloaded the .list files for the server and alternate instal 
> iso images:
>
>     ubuntu-6.06.1-server-i386.list and ubuntu-6.06.1-alternate-i386.list
>
> The ordinary 'desktop' install cd/dvd requires at least 512 MB or ram 
> which I don't have. It is also impossible to download the 700 MB iso 
> images for the server / alternate CDs as I only have a dial-up 
> connection. To make the situation worse, these CDs could not be found in 
> local stores in my city. So the only option left is making the server 
> and alternate CDs myself. This is why I need the script.
>   
That sucks.  It was sounding kind of like a homework problem at first, 
but I believe you.
I hope I helped,
if you have any other questions please ask!
-Luke


More information about the Tutor mailing list