我正在尝试加载存储在 HDFS 中的 Hadoop 集群上的 informatica 日志文件。我在 Python 中使用 subprocess 来执行此操作,但相信由于文件名而出现错误,我不确定如何解决它。
我得到的错误是“cat:非法文件模式:索引 11 附近的非法字符范围”
我的代码是:
input = subprocess.Popen(["hadoop", "fs", "-cat", '/corp_staffs/IT/IICOE/process/infa_stats/WorkflowLogs/infra.[08-04-2015-(15_19)].1438719569664.log'], stdout=subprocess.PIPE)
# read the lines into an array
for line in input.stdout:
print line
我可以重命名每个文件以避免 cat 认为文件名中有正则表达式,但我宁愿不这样做。有没有办法解决这个问题?
最佳答案
quotechars=re.compile('|'.join(re.escape(s) for s in r'\[]()*?'))
def quote_name(filename):
return re.sub(quotechars, r'\\\g<0>', filename)
input = subprocess.Popen(
[
"hadoop", "fs", "-cat",
quote_name('/corp_staffs/IT/IICOE/process/infa_stats/WorkflowLogs/infra.[08-04-2015-(15_19)].1438719569664.log')
], stdout=subprocess.PIPE)
关于使用子进程 hdfs "cat: Illegal file pattern: Illegal character range near index 11"打开 Python HDFS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32055025/