Skip to content

Removing temporary files at the end of execution #1784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3006,6 +3006,8 @@ def load_array(self, name, array):

if self._local:
os.remove(filename)
else:
self.slashdelete(filename)

def load_table(self, name, array, var1="", var2="", var3="", csysid=""):
"""Load a table from Python to into MAPDL.
Expand Down Expand Up @@ -3128,6 +3130,11 @@ def load_table(self, name, array, var1="", var2="", var3="", csysid=""):
# skip the first line its a header we wrote in np.savetxt
self.tread(name, filename, nskip=1, mute=True)

if self._local:
os.remove(filename)
else:
self.slashdelete(filename)

def _display_plot(self, *args, **kwargs): # pragma: no cover
raise NotImplementedError("Implemented by child class")

Expand Down
13 changes: 11 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,12 @@ def sys(self, cmd):
super().sys(f"{cmd} > {tmp_file}")
if self._local: # no need to download when local
with open(os.path.join(self.directory, tmp_file)) as fobj:
return fobj.read()
return self._download_as_raw(tmp_file).decode()
obj = fobj.read()
else:
obj = self._download_as_raw(tmp_file).decode()

self.slashdelete(tmp_file)
return obj

def download_result(self, path=None, progress_bar=False, preference=None):
"""Download remote result files to a local directory
Expand Down Expand Up @@ -1559,9 +1563,12 @@ def input(
# Using CDREAD
option = kwargs.get("cd_read_option", "COMB")
tmp_dat = f"/OUT,{tmp_out}\n{orig_cmd},'{option}','{filename}'\n"
delete_uploaded_files = False

else:
# Using default INPUT
tmp_dat = f"/OUT,{tmp_out}\n{orig_cmd},'{filename}'\n"
delete_uploaded_files = True

if write_to_log and self._apdl_log is not None:
if not self._apdl_log.closed:
Expand Down Expand Up @@ -1605,6 +1612,8 @@ def input(
# Deleting the previous files
self.slashdelete(tmp_name)
self.slashdelete(tmp_out)
if filename in self.list_files() and delete_uploaded_files:
self.slashdelete(filename)

return output

Expand Down
2 changes: 1 addition & 1 deletion tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,13 +1416,13 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
assert "PROPERTY TEMPERATURE TABLE NUM. TEMPS= 1" in output
assert "TEMPERATURE TABLE ERASED." in output
assert "0.4000000" in output
assert fname_ in mapdl.list_files()
# check if materials are read into the db
assert mapdl.get_value("NUXY", "1", "TEMP", 0) == nuxy
assert np.allclose(mapdl.get_value("EX", 1, "TEMP", 0), ex)

# Reding file in remote
fname_ = f"{fname}.{ext}"
mapdl.upload(fname_)
os.remove(fname_)
assert not os.path.exists(fname_)
assert f"{fname}.{ext}" in mapdl.list_files()
Expand Down