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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:53:03+00:00 2026-06-16T10:53:03+00:00

I want to run the following command from within a Python program: python py2.py

  • 0

I want to run the following command from within a Python program:
python py2.py -i test.txt

I tried using subprocess.check_output as follows:

py2output = subprocess.check_output(['python py2.py', '-i', 'test.txt'])
py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
  • 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-16T10:53:04+00:00Added an answer on June 16, 2026 at 10:53 am

    The right answer (using Python 2.7 and later, since check_output() was introduced then) is:

    py2output = subprocess.check_output(['python','py2.py','-i', 'test.txt'])
    

    To demonstrate, here are my two programs:

    py2.py:

    import sys
    print sys.argv
    

    py3.py:

    import subprocess
    py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
    print('py2 said:', py2output)
    

    Running it:

    $ python3 py3.py
    py2 said: b"['py2.py', '-i', 'test.txt']\n"
    

    Here’s what’s wrong with each of your versions:

    py2output = subprocess.check_output([str('python py2.py '),'-i', 'test.txt'])
    

    First, str('python py2.py') is exactly the same thing as 'python py2.py'—you’re taking a str, and calling str to convert it to an str. This makes the code harder to read, longer, and even slower, without adding any benefit.

    More seriously, python py2.py can’t be a single argument, unless you’re actually trying to run a program named, say, /usr/bin/python\ py2.py. Which you’re not; you’re trying to run, say, /usr/bin/python with first argument py2.py. So, you need to make them separate elements in the list.

    Your second version fixes that, but you’re missing the ' before test.txt'. This should give you a SyntaxError, probably saying EOL while scanning string literal.

    Meanwhile, I’m not sure how you found documentation but couldn’t find any examples with arguments. The very first example is:

    >>> subprocess.check_output(["echo", "Hello World!"])
    b'Hello World!\n'
    

    That calls the "echo" command with an additional argument, "Hello World!".

    Also:

    -i is a positional argument for argparse, test.txt is what the -i is

    I’m pretty sure -i is not a positional argument, but an optional argument. Otherwise, the second half of the sentence makes no sense.

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

Sidebar

Related Questions

I want to run the following command from a C program to read the
I want to run the following bash command in Python 3: ls -l I
I'm trying to get my java program to run an svn command from the
I run a Java program with the following command line (Edit: in NetBeans 6.8
I'm trying to run the following command: SELECT * FROM my_table WHERE thecolum =
I have a text file, say, input.txt and I want to run a command
I want to run a groovy command-line script from my Gradle build script. I'm
I want to be able to run the Scrapy web crawling framework from within
i am trying to run following curl command from my ruby code Kernel.system'curl -H
I want to call an external program from Python. I have used both Popen()

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.