Python 的子进程中的 Awk 给出无效表达式“'”错误
Awk in Python's subprocess giving Invalid Expressions "'" error
我正在尝试读取代码中所见的两种命名方案中每一种的最新文件的文件名和文件戳。我有以下代码,大致是:
#!/usr/bin/env python
import string, subprocess, sys, os
mypath = "/path/to/file"
my_cmd = (["ls -lt --full-time " + mypath + "*DAI*.txt",
"ls -lt --full-time " + mypath + "*CA*.txt"]
)
getmostrecent_cmd = "head -n 1"
getcols_cmd = "awk '{ print , , }'"
for cmd in my_cmd:
p1 = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
p2 = subprocess.Popen(getmostrecent_cmd.split(), stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(getcols_cmd.split(), stdin=p2.stdout, stdout=subprocess.PIPE)
output = p3.communicate()[0]
print output
这给我以下错误:
ls: cannot access /path/to/file/*DAI*.txt: No such file or directory
awk: '{
awk: ^ invalid char ''' in expression
ls: cannot access /path/to/file/*CA*.txt: No such file or directory
awk: '{
awk: ^ invalid char ''' in expression
但是:
- 我可以使用“ls -lt --full-time /path/to/file/*DAI*.txt”并在终端中得到结果。为什么它会导致相同路径出现问题?
- awk 命令,当直接放入子进程时,工作正常;例如。 subprocess.Popen(["awk", ....], stdin=...., stdout=....) 工作正常。但是现在我遇到了单引号的问题。我尝试用三重引号引用字符串并转义单引号。
I can use "ls -lt --full-time /path/to/file/DAI.txt" and get a
result in the terminal. Why is it causing an issue with the same path?
全局扩展由 shell 执行。默认情况下,shell 不参与通过 Popen()
启动新的子流程。为此,您必须将 shell=True
参数传递给它:
p1 = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, shell=True)
# ^^^^^^^^^^
The awk command, when put in to subprocess directly, works fine; E.g.
subprocess.Popen(["awk", ....], stdin=...., stdout=....) worked okay. But now I am getting an issue with the single quote. I tried
triple quoting the string and escaping the single-quote.
在 shell 命令行中,需要 awk '{ print , , }'
中的单引号来生成字符串 { print , , }
被视为单个参数(以及防止变量扩展)。 shell去掉单引号,awk
只看到字符串{ print , , }
。由于 Popen()
在执行子进程命令时默认不涉及 shell 并将参数逐字传递给命令,因此不需要单引号:
subprocess.Popen(["awk", "{ print , , }"], stdin=...., stdout=....)
我正在尝试读取代码中所见的两种命名方案中每一种的最新文件的文件名和文件戳。我有以下代码,大致是:
#!/usr/bin/env python
import string, subprocess, sys, os
mypath = "/path/to/file"
my_cmd = (["ls -lt --full-time " + mypath + "*DAI*.txt",
"ls -lt --full-time " + mypath + "*CA*.txt"]
)
getmostrecent_cmd = "head -n 1"
getcols_cmd = "awk '{ print , , }'"
for cmd in my_cmd:
p1 = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
p2 = subprocess.Popen(getmostrecent_cmd.split(), stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(getcols_cmd.split(), stdin=p2.stdout, stdout=subprocess.PIPE)
output = p3.communicate()[0]
print output
这给我以下错误:
ls: cannot access /path/to/file/*DAI*.txt: No such file or directory
awk: '{
awk: ^ invalid char ''' in expression
ls: cannot access /path/to/file/*CA*.txt: No such file or directory
awk: '{
awk: ^ invalid char ''' in expression
但是:
- 我可以使用“ls -lt --full-time /path/to/file/*DAI*.txt”并在终端中得到结果。为什么它会导致相同路径出现问题?
- awk 命令,当直接放入子进程时,工作正常;例如。 subprocess.Popen(["awk", ....], stdin=...., stdout=....) 工作正常。但是现在我遇到了单引号的问题。我尝试用三重引号引用字符串并转义单引号。
I can use "ls -lt --full-time /path/to/file/DAI.txt" and get a result in the terminal. Why is it causing an issue with the same path?
全局扩展由 shell 执行。默认情况下,shell 不参与通过 Popen()
启动新的子流程。为此,您必须将 shell=True
参数传递给它:
p1 = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, shell=True)
# ^^^^^^^^^^
The awk command, when put in to subprocess directly, works fine; E.g. subprocess.Popen(["awk", ....], stdin=...., stdout=....) worked okay. But now I am getting an issue with the single quote. I tried triple quoting the string and escaping the single-quote.
在 shell 命令行中,需要 awk '{ print , , }'
中的单引号来生成字符串 { print , , }
被视为单个参数(以及防止变量扩展)。 shell去掉单引号,awk
只看到字符串{ print , , }
。由于 Popen()
在执行子进程命令时默认不涉及 shell 并将参数逐字传递给命令,因此不需要单引号:
subprocess.Popen(["awk", "{ print , , }"], stdin=...., stdout=....)