Wednesday, October 8, 2008

Update on the python windows saga

The fiasco finally ended, my code is working and the customers are at least quiet. My requirements were simple, my excel trawler trawled the excel pumping stuff into the database. Then after that it had to copy the excel file based on the outcome of the process. So if it failed it had to copy the input excel file into a 'fail' and conversely into a 'pass' folder if it did it's job right. Here is the catch. The customer wanted the whole directory structure of the input file to be created in the fail/pass folders. I thought "Heck this is simple! Just do a 'cp --parents /path/to/input/file /[pass | fail]/folder' What could be easier?"

Boy was I wrong. All of the windblows solutions did not fit right. I found this page by Tim Golden which I thought was golden until I tried out most of it's stuff and it did not exactly work right for me at least. Essentially it boiled down to:

1. If it copied the directory tree(shutil.copytree) it did not copy the file right. If it saw a file in the path, it would just copy the file and the did not do the sub folders and the parent folders. Augh! Maddening!!

2. I could not just do a system call to do 'cp --parents' because this was a windblows machine. So I thought initially os.system stuff was out. It drove me nuts something so simple could not be achieved by any of the windows command line stuff like copy or xcopy or robocopy.

Finally I cheated. I downloaded a windows port of cp, and just a os.system call on it. That too had it's fair share of problems such as you could not do a 'cp --parents c:/some/thing c:/some/one' because it tried to do a c:/some/one/c: and bombed out because it did not like the colon. Looking at this I used the os.path.splitdrive and split up the stuff then did a copy.

I know this is not clean enough for you purist out there, but heck it works and that is all that counts ... to customers at least.

1 comment:

Carl Trachte said...

Working with files on Windows has all kinds of pitfalls. I've had less trouble with directories in the simple solutions I've done than I've had with the timing of file accessibility. Once I create a file for use with a Windows process, I have to sleep five or ten or thirty seconds before it actually becomes available to the program. An alternate super ugly hack I've used is to just run the script twice to make sure the correct new file gets used.
I'm frustrated that file manipulation on Windows is such a bear, but reassured that I'm not the only one . . .