Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Commonmark migration
Source Link

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run(), which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run(), which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run(), which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

added 148 characters in body
Source Link
dimo414
  • 49.4k
  • 19
  • 168
  • 275

First, os.system()os.system() is discouraged in favor of subprocess.call(cmd, shell=True)subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run(), which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run(), which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run(), which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

added 332 characters in body
Source Link
dimo414
  • 49.4k
  • 19
  • 168
  • 275

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run() (or, which returns a call() preCompletedProcess object you can inspect.

As Jean-3François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python.5) It's more verbose, but more extensible and maintainable.

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from call() to one of subprocess other functions that are more scripting-friendly, such as run() (or call() pre-3.5).

First, os.system() is discouraged in favor of subprocess.call(cmd, shell=True). That's worth knowing because there's a lot of additional detail in the subprocess documentation, including this description of the shell=True parameter (emphasis added):

On POSIX with shell=True, the shell defaults to /bin/sh.... Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So now we know why your command doesn't work - it's not invoking Bash. As mhawke suggests you should instead invoke bash directly, but you should prefer the subprocess module over os.system():

>>> subprocess.call(['/bin/bash', '-c', 'diff -u <(echo "aba"| fold -w1) <(echo "abaa" | fold -w1)'])
--- /dev/fd/63 2017年02月25日 14:32:49.000000000 -0800
+++ /dev/fd/62 2017年02月25日 14:32:49.000000000 -0800
@@ -1,3 +1,4 @@
 a
 b
 a
+a
1

Note that, since we're explicitly invoking the Bash shell, we don't need shell=True, and since the command we're telling Bash to invoke is a single argument we don't need to repeatedly escape them, e.g. with """ as mhawke did.

Once you've verified this command works, you'll likely want to move away from simply invoking call() to one of subprocess other functions that are more scripting-friendly, such as run() , which returns a CompletedProcess object you can inspect.

As Jean-François Fabre suggests you can do a lot more powerful things with subprocess as well, including starting the <() substitutions as separate processes and piping them into a call to diff, thus avoiding needing to invoke bash or write Bash syntax in Python. It's more verbose, but more extensible and maintainable.

Source Link
dimo414
  • 49.4k
  • 19
  • 168
  • 275
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /