Skip to content

Commit 326dcc0

Browse files
check if is directory befoe mkdirs (#827)
1 parent f4ca04a commit 326dcc0

File tree

6 files changed

+20
-10
lines changed

6 files changed

+20
-10
lines changed

ppsci/solver/solver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,8 @@ def export(
744744
)
745745

746746
# save static graph model to disk
747-
os.makedirs(osp.dirname(export_path), exist_ok=True)
747+
if osp.isdir(osp.dirname(export_path)):
748+
os.makedirs(osp.dirname(export_path), exist_ok=True)
748749
try:
749750
jit.save(static_model, export_path)
750751
except Exception as e:

ppsci/utils/logger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def init_logger(
104104
# add file_handler, output to log_file(if specified), only for rank 0 device
105105
if log_file is not None and dist.get_rank() == 0:
106106
log_file_folder = os.path.dirname(log_file)
107-
os.makedirs(log_file_folder, exist_ok=True)
107+
if os.path.isdir(log_file_folder):
108+
os.makedirs(log_file_folder, exist_ok=True)
108109
file_formatter = logging.Formatter(
109110
"[%(asctime)s] %(name)s %(levelname)s: %(message)s",
110111
datefmt="%Y/%m/%d %H:%M:%S",

ppsci/utils/symbolic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ def add_edge(u: str, v: str, u_color: str = C_DATA, v_color: str = C_DATA):
615615
graph.layout()
616616
image_path = f"{graph_filename}.png"
617617
dot_path = f"{graph_filename}.dot"
618-
os.makedirs(os.path.dirname(image_path), exist_ok=True)
618+
if os.path.isdir(os.path.dirname(image_path)):
619+
os.makedirs(os.path.dirname(image_path), exist_ok=True)
619620
graph.draw(image_path, prog="dot")
620621
graph.write(dot_path)
621622
logger.message(

ppsci/utils/writer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def save_tecplot_file(
182182
nx, ny = num_x, num_y
183183
assert nx * ny == nxy, f"nx({nx}) * ny({ny}) != nxy({nxy})"
184184

185-
os.makedirs(os.path.dirname(filename), exist_ok=True)
185+
if os.path.isdir(os.path.dirname(filename)):
186+
os.makedirs(os.path.dirname(filename), exist_ok=True)
186187

187188
if filename.endswith(".dat"):
188189
filename = filename[:-4]

ppsci/visualize/plot.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def _save_plot_from_1d_array(filename, coord, value, value_keys, num_timestamps=
8080
value_keys (Tuple[str, ...]): Value keys.
8181
num_timestamps (int, optional): Number of timestamps coord/value contains. Defaults to 1.
8282
"""
83-
os.makedirs(os.path.dirname(filename), exist_ok=True)
83+
if os.path.isdir(os.path.dirname(filename)):
84+
os.makedirs(os.path.dirname(filename), exist_ok=True)
8485
fig, a = plt.subplots(len(value_keys), num_timestamps, squeeze=False)
8586
fig.subplots_adjust(hspace=0.8)
8687

@@ -187,7 +188,8 @@ def _save_plot_from_2d_array(
187188
xticks (Optional[Tuple[float, ...]]): Tuple of xtick locations. Defaults to None.
188189
yticks (Optional[Tuple[float, ...]]): Tuple of ytick locations. Defaults to None.
189190
"""
190-
os.makedirs(os.path.dirname(filename), exist_ok=True)
191+
if os.path.isdir(os.path.dirname(filename)):
192+
os.makedirs(os.path.dirname(filename), exist_ok=True)
191193

192194
plt.close("all")
193195
matplotlib.rcParams["xtick.labelsize"] = 5
@@ -334,7 +336,8 @@ def _save_plot_from_3d_array(
334336
visu_keys (Tuple[str, ...]): Keys for visualizing data. such as ("u", "v").
335337
num_timestamps (int, optional): Number of timestamps coord/value contains. Defaults to 1.
336338
"""
337-
os.makedirs(os.path.dirname(filename), exist_ok=True)
339+
if os.path.isdir(os.path.dirname(filename)):
340+
os.makedirs(os.path.dirname(filename), exist_ok=True)
338341

339342
fig = plt.figure(figsize=(10, 10))
340343
len_ts = len(visu_data[0]) // num_timestamps
@@ -479,7 +482,8 @@ def plot_weather(
479482
)
480483
plt.colorbar(mappable=map_, cax=None, ax=None, shrink=0.5, label=colorbar_label)
481484

482-
os.makedirs(os.path.dirname(filename), exist_ok=True)
485+
if os.path.isdir(os.path.dirname(filename)):
486+
os.makedirs(os.path.dirname(filename), exist_ok=True)
483487
fig = plt.figure(facecolor="w", figsize=(7, 7))
484488
ax = fig.add_subplot(2, 1, 1)
485489
plot_weather(

ppsci/visualize/vtu.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def _save_vtu_from_array(filename, coord, value, value_keys, num_timestamps=1):
5353
if coord.shape[1] not in [2, 3]:
5454
raise ValueError(f"ndim of coord({coord.shape[1]}) should be 2 or 3.")
5555

56-
os.makedirs(os.path.dirname(filename), exist_ok=True)
56+
if os.path.isdir(os.path.dirname(filename)):
57+
os.makedirs(os.path.dirname(filename), exist_ok=True)
5758

5859
# discard extension name
5960
if filename.endswith(".vtu"):
@@ -183,5 +184,6 @@ def save_vtu_to_mesh(
183184
points=points.T, cells=[("vertex", np.arange(npoint).reshape(npoint, 1))]
184185
)
185186
mesh.point_data = {key: data_dict[key] for key in value_keys}
186-
os.makedirs(os.path.dirname(filename), exist_ok=True)
187+
if os.path.isdir(os.path.dirname(filename)):
188+
os.makedirs(os.path.dirname(filename), exist_ok=True)
187189
mesh.write(filename)

0 commit comments

Comments
 (0)