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 986321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:18:07+00:00 2026-05-16T05:18:07+00:00

This is the code I am working with that comes from Practical Programming: import

  • 0

This is the code I am working with that comes from Practical Programming:

 import sys

def process_file(filename):
 '''Open, read, and print a file.'''

 input_file = open(filename, "r")
 for line in input_file:
  line = line.strip()
  print line
 input_file.close()
 if __name__ == "__main__":
  process_file(sys.argv[1])

After import this module in IDLE and pass a text file argument through process_file(), I receive this error:

    Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    process_file("data.txt")
  File "C:\Python26\read_file_2.py", line 14, in process_file
    process_file(sys.argv[1])
IndexError: list index out of range

How can I get this program to work without receiving this error? Any help is appreciated.

  • 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-05-16T05:18:07+00:00Added an answer on May 16, 2026 at 5:18 am

    It looks like you’ve got the

    if __name__ == "__main__":
      process_file(sys.argv[1])
    

    block at the same indentation level as the rest of the process_file definition, so it’s being run when you call process_file from another module. I suspect that might be causing your problem – unindent it so the if is in line with the def.

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

Sidebar

Related Questions

Hi I have been working on this code that creates a table with radio
I've recently been working with code that looks like this: using namespace std; class
I had this working in an old code that I didn’t end up using...
i got this code working and it to return the last tweet from my
I have this simple code that is working fine in every browser, but NOT
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
Why isn't this code working? I've been stuck on this for 2 days. public
I used to have this code working with my Tomcat server: HttpRequestBase targetRequest =
I tried everything to get this code working, and I hope someone will save
Simple question, how make this code working ? public class T { public static

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.