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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:38:53+00:00 2026-06-17T18:38:53+00:00

So, when I run this code in python 2.7.3 with command ./randline.py test.txt this

  • 0

So, when I run this code in python 2.7.3 with command ./randline.py test.txt this code works fine. However when I try to run this code in python 3 I got an error message “/usr/bin/python: can’t open file ‘3’: [Errno 2] No such file or directory”

import random, sys
 from optparse import OptionParser

class randline:
   def __init__(self, filename):
       f = open(filename, 'r')
       self.lines = f.readlines()
       f.close()

   def chooseline(self):
       return random.choice(self.lines)

def main():
   version_msg = "%prog 2.0"
   usage_msg = """%prog [OPTION]... FILE

Output randomly selected lines from FILE."""

   parser = OptionParser(version=version_msg,
                      usage=usage_msg)
   parser.add_option("-n", "--numlines",
                  action="store", dest="numlines", default=1,
                  help="output NUMLINES lines (default 1)")
   options, args = parser.parse_args(sys.argv[1:])

   try:
       numlines = int(options.numlines)
   except:
       parser.error("invalid NUMLINES: {0}".
                    format(options.numlines))
   if numlines < 0:
       parser.error("negative count: {0}".
                 format(numlines))
   if len(args) != 1:
       parser.error("wrong number of operands")
   input_file = args[0]

   try:
       generator = randline(input_file)
       for index in range(numlines):
           sys.stdout.write(generator.chooseline())
   except IOError as (errno, strerror):
       parser.error("I/O error({0}): {1}".
                    format(errno, strerror))

if __name__ == "__main__":
     main()

Is there any thing wrong with this code for python 3 interpreter?

  • 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-17T18:38:54+00:00Added an answer on June 17, 2026 at 6:38 pm

    Looks like you are running python 3 (with a space). You should use python3 instead.

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

Sidebar

Related Questions

/Users/smcho/Desktop/bracket/[10,20] directory has abc.txt, but when I run this Python code import glob import
I try to get whois in python. I use this http://code.google.com/p/pywhois/ but it run
I'm trying to run this code but this error appear: Uncaught TypeError: string is
I am trying to run a Python program using PHP. Here's the code $command
When I run this Python 3.1 code to access a device using telnetlib, it
I want to run python code from C# through command Prompt.The Code is attached
Within my shell script I run this command: python script.py I was wondering, as
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run this code: - (void)unitButtonButtonTapped:(id)sender { [_label setString:@Last button: Unembossed square]; MilitaryUnits *target
Why this code don't work,when i want run this code vwd 2008 express show

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.