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

  • Home
  • SEARCH
  • 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 8268301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:50:19+00:00 2026-06-08T05:50:19+00:00

I have this simple code which is really just to help me understand how

  • 0

I have this simple code which is really just to help me understand how Python I/O works:

inFile = open("inFile.txt",'r')
outFile = open("outFile.txt",'w')
lines = inFile.readlines()

first = True
for line in lines:
    if first == True:
        outFile.write(line)  #always print the header
        first = False
        continue
    nums = line.split()
    outFile.write(nums[3] + "\n") #print the 4th column of each row
outFile.close()

My input file is something like this:

#header
34.2 3.42 64.56 54.43 3.45
4.53 65.6 5.743 34.52 56.4
4.53 90.8 53.45 134.5 4.58
5.76 53.9 89.43 54.33 3.45

The output prints out into the file just as it should but I also get the error:

    outFile.write(nums[3] + "\n")
IndexError: list index out of range 

I’m assuming this is because it has continued to read the next line although there is no longer any data?

  • 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-08T05:50:22+00:00Added an answer on June 8, 2026 at 5:50 am

    Others have already answered your question. Here is a better way to “always print out the file header”, avoiding testing for first at every iteration:

    with open('inFile.txt', 'r') as inFile, open('outFile.txt', 'w') as outFile:
        outFile.write(inFile.readline()) #always print the header
        for line in inFile:
            nums = line.split()
            if len(nums) >= 4: #Checks to make sure a fourth column exists.
                outFile.write(nums[3] + "\n") #print the 4th column of each row
    

    A couple things are going on here:

    with open('inFile.txt', 'r') as inFile, open('outFile.txt', 'w') as outFile:
    

    The with expression is a convenient way to open files because it automatically closes the files even if an exception occurs and the with block exits early.

    Note: In Python 2.6, you will need to use two with statements, as support for multiple contexts was not added until 2.7. e.g:

    with open(somefile, 'r') as f:
        with open(someotherfile, 'w') as g:
            #code here.
    

    outFile.write(inFile.readline()) #always print the header
    

    The file object is an iterator that gets consumed. When readline() is called, the buffer position advances forwards and the first line is returned.


    for line in inFile:
    

    As mentioned before, the file object is an iterator, so you can use it directly in a for loop.

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

Sidebar

Related Questions

i have this simple question please. I have this part of code which sets
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have some really simple code that's just not working: int[] manualPixels = new
I have this simple code (for making things shorted, the important bits are probably
I have this simple code in a WPF application: ThreadStart start = delegate() {
I have this simple code where I am sending http request and reading all
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have this simple code in my user_controller.rb file #listing all users def index
i have this simple code to load data from other php page using get
I have this simple code public String toString() { **Iterator it = list.iterator();** String

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.