我试图将txt文件写入到Cloud Composer DAG中的DAGS文件夹中.文件从来没有出现,我认为我的代码有问题,但我try 以xlsx格式保存Pandas 数据帧,执行DAGs文件夹并加载该数据帧.
事实证明,它奏效了.在相同的代码中,我能够编写Pandas 数据帧,然后在相同的DAG运行中读取它,但后来当我查看文件夹时,没有文件.如果我再次运行代码并try 读取它,它会显示该文件不存在.
就像文件只是暂时写入一样.
我还使用了文件夹的完整路径("/home/airflow/gcs/dgs"),因为我正在try 将文件保存到我的composer 所属的DAGS文件夹中,我想我不应该面临这么多麻烦.
有没有人对我如何解决这个问题有什么 idea ?
编辑:
代码片段:
def _crawl_spiders():
# sets working dir
os.chdir('/home/airflow/gcs/dags/mypath')
df = pd.read_excel('./x-path/sheet.xlsx')
df.to_excel('/home/airflow/gcs/dags/mypath/test.xlsx', index = False)
b = pd.read_excel('/home/airflow/gcs/dags/mypath/test.xlsx')
print(f'Success, b columns:{b.columns}')
with DAG(dag_id="crawler", start_date=datetime(2022,7,28),
schedule_interval='@daily', tags=['muffet', 'crawler']) as dag:
crawl_spiders = PythonOperator(
task_id = 'crawl_spiders',
python_callable = _crawl_spiders,
dag = dag)```