ancient-wolf-19325
07/26/2024, 10:16 AMimport os
from flytekit import task, workflow
from flytekit.types.directory import FlyteDirectory
@task
def t1() -> FlyteDirectory:
dir = os.path.join(os.getcwd(), "results")
os.makedirs(dir, exist_ok=True)
with open(os.path.join(dir, "file1.txt"), "w") as file:
file.write("Content 1\n")
return FlyteDirectory(dir)
@task
def t2() -> FlyteDirectory:
dir = os.path.join(os.getcwd(), "results")
os.makedirs(dir, exist_ok=True)
with open(os.path.join(dir, "file2.txt"), "w") as file:
file.write("Content 2\n")
return FlyteDirectory(dir)
@workflow
def wf() -> FlyteDirectory:
dir1 = t1()
dir2 = t2()
return combine(dir1, dir2)
if __name__ == "__main__":
print(wf())
How should the combine function look like?
# Answer
The combine function should merge the contents of the two FlyteDirectory objects into a single directory. Here's how you can implement it:
1. Create a new directory to hold the combined contents.
2. Copy the contents of the first directory into the new directory.
3. Copy the contents of the second directory into the new directory, ensuring no file name conflicts.
Here's the complete code:
import os
import shutil
from flytekit import task, workflow
from flytekit.types.directory import FlyteDirectory
@task
def t1() -> FlyteDirectory:
dir = os.path.join(os.getcwd(), "results1")
os.makedirs(dir, exist_ok=True)
with open(os.path.join(dir, "file1.txt"), "w") as file:
file.write("Content 1\n")
return FlyteDirectory(dir)
@task
def t2() -> FlyteDirectory:
dir = os.path.join(os.getcwd(), "results2")
os.makedirs(dir, exist_ok=True)
with open(os.path.join(dir, "file2.txt"), "w") as file:
file.write("Content 2\n")
return FlyteDirectory(dir)
@task
def combine(dir1: FlyteDirectory, dir2: FlyteDirectory) -> FlyteDirectory:
combined_dir = os.path.join(os.getcwd(), "combined_results")
os.makedirs(combined_dir, exist_ok=True)
# Copy contents of dir1
for item in os.listdir(dir1.path):
s = os.path.join(dir1.path, item)
d = os.path.join(combined_dir, item)
if os.path.isdir(s):
shutil.copytree(s, d, dirs_exist_ok=True)
else:
shutil.copy2(s, d)
# Copy contents of dir2
for item in os.listdir(dir2.path):
s = os.path.join(dir2.path, item)
d = os.path.join(combined_dir, item)
if os.path.isdir(s):
shutil.copytree(s, d, dirs_exist_ok=True)
else:
shutil.copy2(s, d)
return FlyteDirectory(combined_dir)
@workflow
def wf() -> FlyteDirectory:
dir1 = t1()
dir2 = t2()
return combine(dir1=dir1, dir2=dir2)
if __name__ == "__main__":
print(wf())ancient-wolf-19325
07/26/2024, 10:16 AM>>This code will create two directories,Sources: - https://github.com/flyteorg/flyte/blob/master/docs/user_guide/data_types_and_io/flytedirectory.md - https://github.com/flyteorg/flytesnacks/blob/master/examples/data_types_and_io/data_types_and_io/folder.py Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. Slack Link https://flyte-org.slack.com/archives/CP2HDHKE1/p1721988970.506199 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.andresults1, each containing a file. Theresults2task will merge these directories into a new directory calledcombine.combined_results