Importing OS Commands into Python; how to pipe with a variable in the middle?

driedupsharpie

So here's an example of the terminal line I'm trying to run after importing the OS into a Python Script of mine:

user$ echo variable | thecommand

Even though OS imports have been working for me lately, the fact that the variable is in the MIDDLE of the imported OS command is not allowing my code to run:

#! /bin/python
import os
variable = 'thevariable'
os.system ("echo "+variable +" | thecommand")

the above is what I have tried in a few different syntax's with no success. Is there a way to accomplish what I'm looking to do using the os.system method?

shoosh

Don't use os.system(). it is deprecated. Instead try

import subprocess
variable = 'thevariable'
subprocess.call("echo "+variable +" | thecommand", shell=True)

the shell=True means that the command will be run in a bash process so that echo and the pipe would work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use $EDITOR in the middle of a series of UNIX pipe commands

From Dev

Shell commands with a pipe in Python

From Dev

When pipe linux commands, how to avoid piping python prints

From Dev

How to rename commands (print, os.<...>) in python?

From Dev

How to pipe two commands as parameters?

From Dev

How to add src files in the middle of a pipe

From Dev

How to count the number of lines in the middle of a pipe

From Dev

how to assign variable value in python in terminal commands?

From Dev

How to store pipe ( | ) in a variable?

From Dev

How to pipe to stdin of os.execv'd process in Python

From Dev

How to pipe to stdin of os.execv'd process in Python

From Dev

Importing creating an environment variable in Python?

From Dev

Importing creating an environment variable in Python?

From Dev

How to pipe commands to gnuplot from MATLAB

From Dev

How to pipe command output to other commands?

From Dev

How can I implement pipe for multiple commands?

From Dev

How to pipe output of active stream to other commands

From Dev

How to avoid broken pipe in commands with cat?

From Dev

How to pipe commands to a process running in the background?

From Dev

How to pipe output from legacy commands in powershell?

From Dev

How does pipe work in bash commands?

From Dev

How to assign a variable within a pipe

From Dev

How can I use a "here document" in the middle of a pipe?

From Dev

How to use python variable in os.system?

From Dev

Matching words with a pipe in the middle

From Dev

Python pipe.send() hangs on Mac OS

From Dev

importing variable from module in python makes copy?

From Dev

timeit.timeit variable importing in python

From Dev

How to pass a variable into the middle of $.ajax url with JQuery

Related Related

  1. 1

    Use $EDITOR in the middle of a series of UNIX pipe commands

  2. 2

    Shell commands with a pipe in Python

  3. 3

    When pipe linux commands, how to avoid piping python prints

  4. 4

    How to rename commands (print, os.<...>) in python?

  5. 5

    How to pipe two commands as parameters?

  6. 6

    How to add src files in the middle of a pipe

  7. 7

    How to count the number of lines in the middle of a pipe

  8. 8

    how to assign variable value in python in terminal commands?

  9. 9

    How to store pipe ( | ) in a variable?

  10. 10

    How to pipe to stdin of os.execv'd process in Python

  11. 11

    How to pipe to stdin of os.execv'd process in Python

  12. 12

    Importing creating an environment variable in Python?

  13. 13

    Importing creating an environment variable in Python?

  14. 14

    How to pipe commands to gnuplot from MATLAB

  15. 15

    How to pipe command output to other commands?

  16. 16

    How can I implement pipe for multiple commands?

  17. 17

    How to pipe output of active stream to other commands

  18. 18

    How to avoid broken pipe in commands with cat?

  19. 19

    How to pipe commands to a process running in the background?

  20. 20

    How to pipe output from legacy commands in powershell?

  21. 21

    How does pipe work in bash commands?

  22. 22

    How to assign a variable within a pipe

  23. 23

    How can I use a "here document" in the middle of a pipe?

  24. 24

    How to use python variable in os.system?

  25. 25

    Matching words with a pipe in the middle

  26. 26

    Python pipe.send() hangs on Mac OS

  27. 27

    importing variable from module in python makes copy?

  28. 28

    timeit.timeit variable importing in python

  29. 29

    How to pass a variable into the middle of $.ajax url with JQuery

HotTag

Archive