Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:18:28+00:00 2026-06-07T00:18:28+00:00

I’m trying to write a Python script that uses a particular external application belonging

  • 0

I’m trying to write a Python script that uses a particular external application belonging to the company I work for. I can generally figure things out for myself when it comes to programming and scripting, but this time I am truely lost!

I can’t seem to figure out why the while loop wont function as it is meant to. It doesn’t give any errors which doesn’t help me. It just seems to skip past the important part of the code in the centre of the loop and then goes on to increment the “count” like it should afterwards!

f = open('C:/tmp/tmp1.txt', 'w')    #Create a tempory textfile
f.write("TEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\n") #Put some simple text in there
f.close()   #Close the file

count = 0   #Insert the line number from the text file you want to begin with (first line starts with 0)
num_lines = sum(1 for line1 in open('C:/tmp/tmp1.txt')) #Get the number of lines from the textfile

f = open('C:/tmp/tmp2.txt', 'w')    #Create a new textfile
f.close()   #Close it

while (count < num_lines):  #Keep the loop within the starting line and total number of lines from the first text file
    with open('C:/tmp/tmp1.txt', 'r') as f: #Open the first textfile
        line2 = f.readlines()   #Read these lines for later input
        for line2[count] in f:  #For each line from chosen starting line until last line from first text file,...
            with open('C:/tmp/tmp2.txt', 'a') as g: #...with the second textfile open for appending strings,...
                g.write("hello\n")  #...write 'hello\n' each time while "count" < "num_lines"
    count = count + 1   #Increment the "count"

I think everything works up until: “for line2[count] in f:”

The real code I’m working on is somewhat more complicated, and the application I’m using isn’t exactly for sharing, so I have simplified the code to give silly outputs instead just to fix the problem.

I’m not looking for alternative code, I’m just looking for a reason why the loop isn’t working so I can try to fix it myself.

All answers will be appreciated, and thanking everyone in advance!

Cormac

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:18:30+00:00Added an answer on June 7, 2026 at 12:18 am

    Some comments:

    num_lines = sum(1 for line1 in open('C:/tmp/tmp1.txt'))
    

    Why? What’s wrong with len(open(filename, 'rb').readlines())?

    while (count < num_lines):
        ...
        count = count + 1
    

    This is bad style, you could use:

    for i in range(num_lines):
        ...
    

    Note that I named your index i, which is universally recognized, and that I used range and a for loop.

    Now, your problem, like I said in the comment, is that f is a file (that is, a stream of bytes with a location pointer) and you’ve read all the lines from it. So when you do for line2[count] in f:, it will try reading a line into line2[count] (this is a bit weird, actually, you almost never use a for loop with a list member as an index but apparently you can do that), see that there’s no line to read, and never executes what’s inside the loop.

    Anyway, you want to read a file, line by line, starting from a given line number? Here’s a better way to do that:

    from itertools import islice
    
    start_line = 0 # change this
    filename = "foobar" # also this
    
    with open(filename, 'rb') as f:
        for line in islice(f, start_line, None):
            print(line)
    

    I realize you don’t want alternative code, but your code really is needlessly complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.