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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:02:50+00:00 2026-06-12T04:02:50+00:00

Assume a python script is callable with: python myscript.py ./mycommand > myoutput.txt How do

  • 0

Assume a python script is callable with:

python myscript.py ./mycommand > myoutput.txt

How do I make sure that myscript.py gets ./mycommand > myoutput.txt in the list of command line arguments rather than the shell piping the output of python myscript.py ./mycommand to the file?

EDIT

Using quotes poses additional problems. Consider:

import subprocess
import sys

argList = sys.argv[1].split()

subprocess.call(argList)

which is then called by:

python myscript.py 'ls -l > ls.out'

results in

ls: cannot access >: No such file or directory
ls: cannot access ls.out: No such file or directory

The motivation is that only the output from the command I am trying to run should be saved in the file. Any other output from myscript.py to stdout shouldn’t go to the file. It would appear that the pipe needs to be set with the stdout option to subprocess.call(). Is there a way to use the list of arguments directly so I don’t have to parse it for things like >, 2>, 2&>, < etc?

  • 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-12T04:02:51+00:00Added an answer on June 12, 2026 at 4:02 am

    A standard bash or tcsh shell will parse the command you give it itself, and interpret certain special characters as direction to the shell process itself (the “>” character in your example). The called program has no control over this; the command line parsing happens before your program is called.

    In your example, you could say:

    python myscript.py ./mycommand ">" myoutput.txt
    

    And the quoted “>” will tell the shell not to use its special behavior.

    The subprocess module has two modes of operation; one with a shell (typically /bin/sh) intermediate process — this is when you give the Popen object a shell=True keyword argument. When you invoke it in this way, it invokes the shell to parse the argument text, and then the shell invokes the command you specified as a subprocess-once-removed from your program.

    The other mode of operation, the default, is when shell=False. This creates a subprocess and populates its ARGV array directly without any intermediate shell trying to interpret the characters in your command.

    The end result is, if you want to issue a command from a shell that interprets special characters, and include special characters, you have to escape them or quote them. If you’re invoking a subprocess from the subprocess standard library module, you can pass in arbitrary text as the elements of the subprocess-to-be’s ARGV, and not worry about them being interpreted in some special way by an intermediary shell process.

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

Sidebar

Related Questions

Let's assume I have a main script, main.py, that imports another python file with
I want my Python script to be able to read Unicode command line arguments
Let's assume I'm stuck using Python 2.6, and can't upgrade (even if that would
Lets assume that we have PyV8: import PyV8 ctxt = PyV8.JSContext() and a python
I have a makefile that invokes a python script that lives in the same
I'm trying to make a Python script run another program from its own path.
I am looking for small bash or python script that will install a .dmg
Assume I have a file at http://mysite.com/myscript.sh that contains: #!/bin/bash echo Hello $1 From
I have a custom python script that monitors the call logs from a Nortel
I have a quick one off task in a python script that I'd like

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.