0

I have the following section of code that works perfectly when I run it as a .py file. When I convert it to a .exe using py2exe it appears that the linecache.getline() function is not working properly. I am able to create and write the .txt file fine, so I believe the linecache function is the issue. Is there any workaround for this that does not use linecache to read a specified line from a textfile or is there a way to get linecache to work with Py2exe?

server = "en15"
pagenum = "0"
page = urllib.urlopen('http://www.westforts.com/%s/battles/page/%s' % (server, pagenum))
page_content = page.read()
with open('battle_id_getter%s.txt' % (pagenum) , 'w') as textfile:
    textfile.write(page_content)
line = linecache.getline('battle_id_getter%s.txt' % (pagenum), 126)

Thanks in advance for any assistance you can provide.

2
Contributors
3
Replies
29
Views
1 Year
Discussion Span
Last Post by Gribouillis
1

In the heart of the linecache module are these two lines

        with open(fullname, 'rU') as fp:
            lines = fp.readlines()

You could use this to read the file's contents.

0

Thank-you Gribouillis!

Changed it to the code below and worked like a charm!

    page = urllib.urlopen('http://www.westforts.com/%s/battles/page/%s' % (server, pagenum))
    page_content = page.read()
    with open('battle_id_getter%s.txt' % (pagenum) , 'w') as textfile:
        textfile.write(page_content)
    with open('battle_id_getter%s.txt' % (pagenum), 'rU') as fp:
        lines = fp.readlines()
    line = lines[125]
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.