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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:44:59+00:00 2026-06-08T13:44:59+00:00

when executing on the command line it is supposed to have python3 a4.py apple

  • 0

when executing on the command line it is supposed to have python3 a4.py apple

the file whose numbers i am trying to sum looks exactly like this

4   14
5

this is what i have gathered so far

import sys
print(sys.argv[1])
fp = open(sys.argv[1])
fileContents = fp.read()
fp.close()
numbers = fileContents
print(numbers)

def map(f,items):
    result = []
    for i in range(0,len(items),1):
        result = result + [f(items[i])]
    return result

a=map(eval,numbers)

def sum(num):
    total = 0
    for i in range(0,len(num),1):
        total = total + i
    return total
print(sum(a))
  • 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-08T13:45:00+00:00Added an answer on June 8, 2026 at 1:45 pm

    (I am a bit confused by the “recursion” tag)

    How about this?

    import sys
    
    with open(sys.argv[1]) as inf:
        total = 0
        for line in inf:
            total += sum(int(i) for i in line.split())
    
    print(total)
    

    Notes:

    • this assumes that each line only contains numeric values (in this case integers)
    • if your numbers contain float values, use float() rather than int()
    • using with automatically closes the file for you when you are done or you encounter an execption.
    • this processes the file line-by-line, so it will not be limited by memory issues with huge files (should that ever be a possibility)

    Update:

    You can shorten this even more with this provided by a helpful comment by J.F.Sebastian below:

    with open(sys.argv[1]) as inf:
        print(sum(int(number) for line in inf for number in line.split()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can have post build command line events... executing a batch file,
I am trying to install BDE Engine by executing the following command line from
Say I have a command line C program which is currently executing, and I
I have this query which on executing in my sql command line client executes
I have a .jar file which has a command line interface. I want to
When executing a Powershell script (in 2.0) using the -File command line switch, and
We have a command line exe that takes input from a text file and
I have a following problem. I am executing an OS command line from within
I have this error in eclipse helios: Exception occurred executing command line. Cannot run
I have an Android activity where I'm executing NDK compiled code (command line program)

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.