Re: [Tutor] error in writelines
LL wrote: Hi All, I have a list containing 108 filenames. I want to open each file and write it to an output file, appending it to the previous write. I use the code below. Everything appears to work fine until I reach file 107. Only part of the file is written, and file 108 is not written at all. The program runs to completion (I get exit code 0). I debugged the problem and found that readlines() is reading all of file 107's contents but that writelines() is not writing all of file 107's contents. There are no strange characters where the write stops. Any advice would be greatly appreciated. Thanks, Lance - fileobjectw = open(COMPILETRACEDIR + '\\IncludeCode.prg', 'w') for line in listext: line = string.strip(line) fileobjectr = open(line, 'r') sa = fileobjectr.readlines() fileobjectr.close() fileobjectw.writelines(sa) fileobjectw.close() Sounds like maybe you should just do a binary read - write. like, outputfile = file(os.path.join(COMPILETRACEDIR,'IncludeCode.prg'),'wb') tmp = file('filenames.txt','r') filenames = [a.strip() for a in tmp.readlines()] tmp.close() for filename in filenames: inputfile = file(filename,'rb') outputfile.write(inputfile.read()) inputfile.close() outputfile.close() I think that should work. I don't have the files you're running this all on, though. It might help if you gave us the text of a successful file and of 107. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] please do not post question about writelines
LL wrote: Hi... I sent a question about an apparent error with writelines. I discovered my error (not closing the file correctly). Please don't post my question. Oops, I didn't read this e-mail before I sent my reply to your other e-mail. :). hope that it helps you anyway. Also, whenever you e-mail tutor@python.org it automatically sends to us all, there's not some middleman who reads over the e-mails. It seemed to me like you thought this was happening from your 'please don't post my question' comment. HTH, -Luke Thanks, Lance ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Dates
Alan, Thanks for the help. I have converted the dates to strings but I get the same error. Please see the updated code below, is this what you meant by converting the dates to strings? import mx.ODBC import mx.ODBC.Windows import mx.DateTime import datetime a = datetime.date(2006,01,31) b = datetime.date(2006,12,31) c = str(a) d = str(b) print a,b,c,d db = mx.ODBC.Windows.DriverConnect('DSN=tnt') c = db.cursor() c.execute('SELECT * FROM times where rt_weekst = ? and rt_weekst = ? and rt_type == ?', (c,d,R,)) for row in c.fetchall(): print row row = str(row) c.close() The output is below: 2006-01-31 2006-12-31 2006-01-31 2006-12-31 Traceback (most recent call last): File C:\test\timemanager.py, line 18, in ? c.execute('SELECT * FROM times where rt_weekst = ? and rt_weekst = ? and rt_type == ?', (c,d,R,)) DataError: ('22005', 301, '[Microsoft][ODBC Visual FoxPro Driver]Operator/operand type mismatch.', 4579) Is there another way I can approach this problem? Say if I use something like: c.execute('SELECT * FROM times where rt_weekst = date(?) and rt_weekst = date(?) and rt_type == ?', (c,d,R,)) I get the following error: 2006-01-31 2006-12-31 2006-01-31 2006-12-31 Traceback (most recent call last): File C:\test\timemanager.py, line 18, in ? c.execute('SELECT * FROM times where rt_weekst = date(?) and rt_weekst = date(?) and rt_type == ?', (c,d,R,)) ProgrammingError: ('37000', 229, '[Microsoft][ODBC Visual FoxPro Driver]Too few arguments.', 4579) Thanks for any suggestions. John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tempfile and webbrowser
Kent Johnson a écrit : Hello, Try it like this, using os.fdopen() to convert the low-level file handle from mkstemp() to a Python file object: In [21]: fd, fname = tempfile.mkstemp() In [22]: f = os.fdopen(fd, 'w') In [23]: f.write('foo') In [24]: f.close() In [25]: os.unlink(fname) Seems to work... Yes, indeed, it works. Not so easy for me to understand, though. I think I get it, more or less, with the help of the Python tempfile module documentation and the help of the Wikipedia article on file descriptors: http://en.wikipedia.org/wiki/File_descriptor Thank you. -- Yves Egrix ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dates
a = datetime.date(2006,01,31) b = datetime.date(2006,12,31) c = str(a) d = str(b) I'm not sure what format your data base expects for dates but given you used the SQL date function before I'd go for that again. Just pass the string equivalent of your dates into the SQL date function. You probavly don;t need the datetime stuff above at all. Is there another way I can approach this problem? Say if I use something like: c.execute('SELECT * FROM times where rt_weekst = date(?) and rt_weekst = date(?) and rt_type == ?', (c,d,R,)) Yes this is what I mean but to male it work c and d need to be in the same format you had before, something like c = 2006,01,31 That will then be inserted into the query string. I'm not sure if it will work like that, you may need to split it into 3 separate parameters. Driver]Too few arguments.', 4579) I think thats because it sees the x-y-z format as an arithmetic sum or as a single non standard string. ButIi'm guessing a bit here as I've never used the ODBC driver nor Foxbase. HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tempfile and webbrowser
yves wrote: Kent Johnson a écrit : Hello, Try it like this, using os.fdopen() to convert the low-level file handle from mkstemp() to a Python file object: In [21]: fd, fname = tempfile.mkstemp() In [22]: f = os.fdopen(fd, 'w') In [23]: f.write('foo') In [24]: f.close() In [25]: os.unlink(fname) Seems to work... Yes, indeed, it works. Not so easy for me to understand, though. I think I get it, more or less, with the help of the Python tempfile module documentation and the help of the Wikipedia article on file descriptors: http://en.wikipedia.org/wiki/File_descriptor OK...the problem was that mkstemp() was opening the file and returning a low-level object that references the open file. You were opening the file a second time, so it would have to be closed twice before it could be deleted. The object returned by mkstemp is actually just an integer called a file handle. This is the way C refers to open files. The call to os.fdopen() wraps the low-level file handle with a Python file object which you can then use just as if you opened it yourself. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dates
John CORRY wrote: Hi All, I am using the code below to select items from a visual foxpro database where the dates are between the 31/01/2006 and 31/12/2006. The good news is that the code below works. However, I want to make the from and to dates variable. I want to change the range depending on user input. I can't get this to work. I have tried the code below marked Tried but I get the error: Traceback (most recent call last): File C:\test\timemanager.py, line 16, in ? c.execute('SELECT * FROM times where rt_weekst = ? and rt_weekst = ? and rt_type == ?', (a,b,R,)) DataError: ('22005', 301, '[Microsoft][ODBC Visual FoxPro Driver]Operator/operand type mismatch.', 4579) Code that works is below: import mx.ODBC import mx.ODBC.Windows import mx.DateTime db = mx.ODBC.Windows.DriverConnect('DSN=tnt') c = db.cursor() c.execute('SELECT * FROM times where rt_weekst = date(2006,01,31) and rt_weekst = date(2006,12,31) and rt_type == ?', (R,)) for row in c.fetchall(): print row row = str(row) c.close() Tried but get errors: import mx.ODBC import mx.ODBC.Windows import mx.DateTime import datetime a = datetime.date(2006,01,31) b = datetime.date(2006,12,31) db = mx.ODBC.Windows.DriverConnect('DSN=tnt') c = db.cursor() c.execute('SELECT * FROM times where rt_weekst = ? and rt_weekst = ? and rt_type == ?', (a,b,R,)) for row in c.fetchall(): print row row = str(row) c.close() Is there a way to format the date so that the Select statement works? a = date(%i,%i,%i) % (2006,01,31) b = date(%i,%i,%i) % (2006,12,31) sql = 'SELECT * FROM times where rt_weekst = %s and rt_weekst = %s and rt_type = ' % (a,b,R) c.execute(sql) OR you could shorten it a bit: sql = 'SELECT * FROM times where rt_weekst between %s and %s and rt_type = %s' % (a,b,R) I like to assign sql first, then execute it, as I can inspect sql if there is a problem. FoxPro also recognizes date constants like {12/31/2006} (note braces not parentheses). -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Dates
Bob and Alan, Thanks for the help. I have gone with the following code and it works! a = date(%i,%i,%i) % (2006,01,31) b = date(%i,%i,%i) % (2006,12,31) sql = 'SELECT * FROM times where rt_weekst = %s and rt_weekst = %s and rt_type = %s ' % (a,b,R,) db = mx.ODBC.Windows.DriverConnect('DSN=tnt') c = db.cursor() c.execute(sql) As you rightly pointed out, I needed to get my sql string formatted and working before putting it anywhere near the c.execute command. Many thanks, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor