[英]Variables in python regex? Print statements in airflow?
我正在開發一個簡單的程序,以使用氣流查看目錄中是否存在文件“ test-2018-06-04-1358.txt”。 我有兩個問題。
A)我想在正則表達式中使用變量datestr。 不知道該怎么做。
B)其次,我的打印(文件名)在氣流UI中顯示在哪里? 我查看了查看日志,但沒有任何顯示。
def checksFile():
d = datetime.today()-timedelta(days=1)
datestr = '{:%Y-%m-%d}'.format(d)
for filename in os.listdir('/mnt/volume/home/aabraham/'):
match = re.search('(test)-(2018-06-04)-(\d+)(\.txt)', filename)
print(filename)
if not match:
raise AirflowException("File not Found")
要回答正則表達式問題,只需將字符串加在一起:
match = re.search('(test)-(' + datestr + ')-(\d+)(\.txt)', filename)
僅當datestr
不包含任何正則表達式文字時,此方法才有效。
您不能以與控制台相同的方式使用print
。
要在“ Log
頁面中查看日志記錄條目,請使用logging.info
。 也許您需要import logging
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.