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.

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.

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]

great ;) !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.