script question

0 views
Skip to first unread message

Scott Martin

unread,
Mar 2, 2010, 2:06:40 AM3/2/10
to orlan...@googlegroups.com
I'm expecting this script to print every line from a textfile, but its
only printing the last line from the file. Any help appreciated.

myfile = open('ilist.txt')
for line in myfile.readlines():
print line

Best,
Scott

Bill Evans

unread,
Mar 2, 2010, 9:09:18 AM3/2/10
to orlan...@googlegroups.com
readlines() returns a list of the lines in the file, a more memory
efficient way would be to just loop over the file object itself:

for line in myfile:
print line

--
Bill Evans

squareFACTOR
bi...@squarefactor.com
office:407.637.2419

> --
> You received this message because you are subscribed to the Google Groups "Orlando Python Users Group" group.
> To post to this group, send email to orlan...@googlegroups.com.
> To unsubscribe from this group, send email to orlando-pug...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/orlando-pug?hl=en.
>
>

Chad MILLER

unread,
Mar 2, 2010, 10:15:35 AM3/2/10
to orlan...@googlegroups.com


There's nothing incorrect with that. Check your assumptions. - chad

Scott Martin

unread,
Mar 2, 2010, 10:25:15 AM3/2/10
to orlan...@googlegroups.com
>> myfile = open('ilist.txt')
>> for line in myfile.readlines():
>>        print line
>
>
> There's nothing incorrect with that.  Check your assumptions.  - chad

Yes, I redirected the output to a file and indeed it is working.

I wasn't explicit about what I'm trying to do. My textfile is a
newline delimited list of approximately 30000 .jpg names to which i
want to prepend a URL and download. This is what I have now:

import sys


myfile = open('ilist.txt')
for line in myfile.readlines():

sys.stdout.write('http://BASEURL/')
print line


Which results in this output:

http://BASEURL/6401287.jpg
6401288.jpg
6403421.jpg
6403422.jpg
6403423.jpg
6403426.jpg
6406701.jpg
6407044.jpg

Brand new to Python, reading about how the for loop works. Any help appreciated!

Chad MILLER

unread,
Mar 2, 2010, 10:33:09 AM3/2/10
to orlan...@googlegroups.com
On Tue, Mar 2, 2010 at 10:25, Scott Martin <ema...@gmail.com> wrote:
> I wasn't explicit about what I'm trying to do. My textfile is a
> newline delimited list of approximately 30000 .jpg names to which i
> want to prepend a URL and download. This is what I have now:
>
> import sys
> myfile = open('ilist.txt')
> for line in myfile.readlines():
>        sys.stdout.write('http://BASEURL/')
>        print line


import urllib

with open('ilist.txt') as myfile:
for line in myfile:
url = "http://%s/%s" % (hostname, urllib.quote(line.rstrip("\n")),)
download(url) # elftr

dumbshow

unread,
Mar 2, 2010, 10:45:15 AM3/2/10
to Orlando Python Users Group
Scott, you should use urllib.urlretrieve

>>> import urllib
>>> urllib.urlretrieve("http://www.google.com/intl/en_ALL/images/logo.gif", "logo.gif")
('logo.gif', <httplib.HTTPMessage instance at 0x012F6288>)

this downloads logo.gif to the current directory ( and you can find
out the current directory in with os.getcwd() )

documentation is here: http://docs.python.org/library/urllib.html

On Mar 2, 10:33 am, Chad MILLER <chadm...@gmail.com> wrote:

Chris Stewart

unread,
Mar 2, 2010, 1:00:23 PM3/2/10
to orlan...@googlegroups.com
Not to get away from Python, but if you aren't just experimenting with
Python, 'wget' is probably a better method for downloading so many
files. It will almost certainly be faster, and will take care of a
lot of the dirty work (how to handle existing output files, partial
downloads, etc.). I only advocate this method if you are trying to
get this done quickly, because you won't learn much.

Make an output file of URLs with Python, and then read up on wget's '-i' option.

-chris

Scott Martin

unread,
Mar 2, 2010, 5:24:54 PM3/2/10
to orlan...@googlegroups.com
On Tue, Mar 2, 2010 at 1:00 PM, Chris Stewart <cstewa...@gmail.com> wrote:

> Make an output file of URLs with Python, and then read up on wget's '-i' option.

This is what I ended up doing. The problem was my textfile was
formatted with ^M as the newline character and the python script was
treating the entire file as one line.

Thanks everyone for your help.

Chad MILLER

unread,
Mar 2, 2010, 7:53:35 PM3/2/10
to orlan...@googlegroups.com

For five years, Python has had universal-newline support. It should
understand all of \r, \n, and \r\n idioms, for most normal
line-oriented file-operations. file.readlines() is one the the dark
corners that I don't use or know about, but I am pretty sure that,
regardless of newline idiom, a typically compiled Python would have
done the right thing with

for line in file("foo"):
f(line)

Reply all
Reply to author
Forward
0 new messages