我正在尝试加载存储在HDFS中的Hadoop集群上的informatica日志文件。我在Python中使用子进程来执行此操作,但我认为由于文件名而出现错误,我不知道如何解决它。
我得到的错误是“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
我可以重命名每个文件,以避免猫认为文件名中有正则表达式,但我宁愿不这样做。有办法解决这个问题吗?
答案 0 :(得分:1)
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)