[Tutor] Writing to a file problem....

wesley chun wescpy at gmail.com
Fri Dec 5 10:37:56 CET 2008


marty,

i applaud you in your efforts to port this script to the Win32
platform. the task is not as simple as one may expect, due to the
differing file pathname nomenclatures that the different operating
systems use.

because of this, i have a couple of suggestions:

1. highly recommend converting this script to use the built-in ZIP
file support offered by Python via the zipfile module:
http://docs.python.org/lib/module-zipfile.html

2. instead of filenames like: 'c:\Users\Marty\Downloads',
'c:\Users\Marty\Contacts',  'J:\\backup\\', etc., i would recommend
using "raw strings." these will help deal with the backward slashes.
in the 3rd example, you correctly use double-backslashes, but you
don't in the 1st pair.  for all 3, change all the files to single
backslashes and place an "r" preceding the opening quote, i.e.,
r'c:\Users\Marty\Downloads', r'c:\Users\Marty\Contacts', r'J:\backup'.
for the last case where you are using a directory/folder name, use
os.path.join() instead of manually placing the filepath separator.

if you combine these together, you should be able to rework your
script so that things work properly without having to use external
calls like os.system().

hope these help!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list