On 12/12/2009 1:52 AM, Beth Cimini wrote:
> I usually
> get one of two errors (without the try/except statement of course):
> If using "from xlutils.copy import copy"
> Traceback (most recent call last):
> File "C:\Python26\TT3.py", line 24, in <module>
> tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5')
Nothing to do with your current problem, but the above statement shows
symptoms of 3 or 4 bad habits which will bite you one day if you don't
cure yourself:
(1) Keep your own data files etc well away from the directories in which
third-party software is installed. Put them in directories with
meaningful names. Consider what you will do when you upgrade to Python 2.7.
(2) Try to avoid hard-coding file paths in your scripts. If you must do
it, you need to bear in mind that when using Python on Windows, the
backslash character is overloaded; it's both a path separator
(C:\foo\bar.zot) and an escape character used to represent control
characters etc (\r\n\t). By the time that Windows gets the first
argument, that \t will be a TAB ('\x09'), not a backslash followed by a
't' ... consquently, the path will be invalid.
There are 3 ways around this (apart from not hard-coding at all):
| >>> a = 'C:\Python26\trash.xls'
| >>> b = r'C:\Python26\trash.xls'
| >>> c = 'C:\\Python26\\trash.xls'
| >>> d = 'C:/Python26/trash.xls'
| >>> map(len, [a,b,c,d])
| [20, 21, 21, 21]
| >>> a == b
| False
| >>> b == c
| True
| >>> open(a)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 22] invalid mode ('r') or filename:
'C:\\Python26\trash.xls'
| >>> open(b)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 2] No such file or directory: 'C:\\Python26\\trash.xls'
| >>> open(c)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 2] No such file or directory: 'C:\\Python26\\trash.xls'
| >>> open(d)
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| IOError: [Errno 2] No such file or directory: 'C:/Python26/trash.xls'
| >>>
(3) The script snippet you posted first didn't use the first (inbook)
arg to that function
and/or
(4) You may not be posting that you actually ran.
> File "C:\Python26\TT3.py", line 18, in tofile
> w=xlutils.copy.copy(wb)
> NameError: global name 'xlutils' is not defined
If as you say you have done
from xlutils.copy import copy
then you should call it by doing
w = copy(wb)
HTH,
John