Re: how to read the last line of a huge file???

2011-03-04 Thread tkp...@hotmail.com
I've implementing this method of reading a file from the end, i.e

def seeker(filename):
offset = -10
with open(filename) as f:
while True:
f.seek(offset, os.SEEK_END)
lines = f.readlines()
if len(lines) >= 2:
return lines[-1]
offset *= 2

and consistently run into the following error message when Python 3.2
(running under Pyscripter 2.4.1) tries to execute the line
f.seek(offset,2)

UnsupportedOperation: can't do non-zero end-relative seeks

But offset is initialized to -10. Does anyone have any thoughts on
what the error might be caused by?

Thanks in advance

Thomas Philips


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to read the last line of a huge file???

2011-03-05 Thread tkp...@hotmail.com
Thanks for the pointer. Yes, it is a text file, but the mystery runs
deeper: I later found that it works perfectly as written when I run it
from IDLE or the Python shell, but it fails reliably when I run it
from PyScripter 2.4.1 (an open source Python IDE)! So I suspect
there's a PyScripter issue lurking in here. I'm next going to try the
solution you propose - use only for legal offsets - and then retry it
under both IDLE and PyScripter. Question: how do I use f.tell() to
identify if an offset is legal or illegal?

Thanks in advance


Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to read the last line of a huge file???

2011-03-10 Thread tkp...@hotmail.com
There is a problem, and it's a Python 3.2 problem. All the solutions
presented here work perfectly well in Python 2.7.1, and they all fail
at exactly the same point in Python 3.2 - it's the line that tries to
seek from the end. e.g.
f.seek(offset, os.SEEK_END)

I'll register this as a Python bug. Thank you, everyone, for the help
and guidance.

Sincerely


Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 3.2 bug? Reading the last line of a file

2011-05-25 Thread tkp...@hotmail.com
The following function that returns the last line of a file works
perfectly well under Python 2.71. but fails reliably under Python 3.2.
Is this a bug, or am I doing something wrong? Any help would be
greatly appreciated.


import os

def lastLine(filename):
'''
Returns the last line of a file
file.seek takes an optional 'whence' argument which allows you
to
start looking at the end, so you can just work back from there
till
you hit the first newline that has anything after it
Works perfectly under Python 2.7, but not under 3.2!
   '''
offset = -50
with open(filename) as f:
while offset > -1024:
offset *= 2
f.seek(offset, os.SEEK_END)
lines = f.readlines()
if len(lines) > 1:
return lines[-1]

If I execute this with a valid filename fn. I get the following error
message:

>>> lastLine(fn)
Traceback (most recent call last):
  File "", line 1, in 
lastLine(fn)
  File "", line 13, in lastLine
f.seek(offset, os.SEEK_END)
io.UnsupportedOperation: can't do nonzero end-relative seeks

Sincerely

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 bug? Reading the last line of a file

2011-05-25 Thread tkp...@hotmail.com
Thanks for the guidance - it was indeed an issue with reading in
binary vs. text., and I do now succeed in reading the last line,
except that I now seem unable to split it, as I demonstrate below.
Here's what I get when I read the last line in text mode using 2.7.1
and in binary mode using 3.2 respectively under IDLE:

2.7.1
Name31/12/2009  0   0   0

3.2
b'Name\t31/12/2009\t0\t0\t0\r\n'

if, under 2.7.1 I read the file in text mode and write
>>> x = lastLine(fn)
I can then cleanly split the line to get its contents
>>> x.split('\t')
['Name', '31/12/2009', '0', '0', '0\n']

but under 3.2, with its binary read, I get
>>> x.split('\t')
Traceback (most recent call last):
  File "", line 1, in 
x.split('\t')
TypeError: Type str doesn't support the buffer API

If I remove the '\t', the split now works and I get a list of bytes
literals
>>> x.split()
[b'Name', b'31/12/2009', b'0', b'0', b'0']

Looking through the docs did not clarify my understanding of the
issue. Why can I not split on '\t' when reading in binary mode?

Sincerely

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 bug? Reading the last line of a file

2011-05-27 Thread tkp...@hotmail.com
This is exactly what I want to do - I can then pick up various
elements of the list and turn them into floats, ints, etc. I have not
ever used decode, and will look it up in the docs to better understand
it. I can't thank everyone enough for the generous serving of help and
guidance - I certainly would not have discovered all this on my own.

Sincerely


Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Working with databases (ODBC and ORMs) in Python 3.2

2011-11-10 Thread tkp...@hotmail.com
We are in the process of trying to decide between Python 2.7 and 3.2
with a view to making a 5-10 year commitment to the right platform,
and would appreciate some guidance on how best to connect to SQL
databases in 3.2. ceODBC 2.01 provides an ODBC driver for Python 3.2,
does anyone have experience using it? Also, are there any ORMs (object
relational mapper)s that work well with 3,2?

Thanks in advance

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: "Best" book for teaching

2009-04-08 Thread tkp...@hotmail.com
I taught myself Python from Python Programming for the Absolute
Beginner by Michael Dawson (which has been mentioned above) and with
lots of help from the friendly members of this group, but there's now
a free e-book titled Snake Wrangling for Kids by Jason Briggs. You can
view it at http://www.briggs.net.nz/log/writing/snake-wrangling-for-kids/.
There are versions for Windows, Mac and Linux, though it is focused on
Python 2.x.

Thomas Philips
--
http://mail.python.org/mailman/listinfo/python-list


Uploading files to an an FTP site through a proxy server which requires authentication

2011-02-09 Thread tkp...@hotmail.com
I would like to upload files to a ftp site, but can't seem to get
through our proxy server, which requires authentication. How can one
do this in Python (I use 2.7, but am more than happy to use 3.2 rc2)

Thanks in advance

Thomas Philips

-- 
http://mail.python.org/mailman/listinfo/python-list


Startup problems with Python 3.1.2 and PythonPath under XP

2010-04-12 Thread tkp...@hotmail.com
I run Python under Windows XP SP3, and for the longest time, I have
installed it on my C: drive under C:\PythonXX (XX = 20, 21., 26),
and maintained all my Python files on our network in a directory
called H:\Python that I point to by creating an environment variable
called PYTHONPATH. I recently installed 3.1.2 under C:\Python31, and
now IDLE crashes every single time Python 3.1 starts up. If I remove
PYTHONPATH, it starts up without a problem. The mystery deepens -
Python 2.6 starts without a hiccup with or without PYTHONPATH! What
could the issue be? Is there any other way to have H:\Python appended
to sys.path when I start Python 3.1?

Thank you in advance for your help and advice

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Startup problems with Python 3.1.2 and PythonPath under XP

2010-04-12 Thread tkp...@hotmail.com
I fixed the problem by creating a file call MyPath.pth that has only
one line
H:/Python

and placing it in the C:\Python31\Lib\site-packages directory. So as a
practical matter, my problem is solved. That said, I'm still puzzled
by why Python 3.1 acts up when I set the environment variable
PYTHONPATH. It certainly caused no problem with the 2.X series.

-- 
http://mail.python.org/mailman/listinfo/python-list


Problems with extra blank line when using csv.writer in Python 3.1

2010-04-27 Thread tkp...@hotmail.com
I’m experiencing a problem with the csv module in Python 3.1.2, and
would greatly appreciate any help anyone can offer me. When writing
csv files in Python 2.6, I open the output file as 'wb' to prevent a
blank line being inserted after every line. Works like a charm. But I
get an error if I do the same in 3.1; i.e if type the following
sequence of commands:
>>> import csv
>>> outfile = open('c:/temp/test.csv', 'wb')
>>> writer = csv.writer(outfile)
>>> line = [1, 2, 3, 4]
>>> writer.writerow(line)
Traceback (most recent call last):
  File "", line 1, in 
writer.writerow(line)
TypeError: must be bytes or buffer, not str


Switching to
>>> outfile = open('c:/temp/test.csv', 'w')

makes it work, but I now have a blank line after each line when I open
the file using Excel. Any thoughts or guidance on what I ought to be
doing in 3.1 to fix the extra blank line problem?

Thank you in advance

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with extra blank line when using csv.writer in Python 3.1

2010-04-27 Thread tkp...@hotmail.com
Worked like a charm! Thank you very much.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python needs a tutorial for install and setup on a Mac

2009-06-22 Thread tkp...@hotmail.com
I think a setup guide for the Mac would prove very useful. Earlier
this year, I tried installing Python 2.6 on my iMac, and ran into all
sorts of problems, largely as a result of the fact that I knew very
little about Unix. I finally downloaded and installed the Enthought
Python distribution for the Mac and it worked like a charm.
-- 
http://mail.python.org/mailman/listinfo/python-list