Skip to content

Commit a470a63

Browse files
authored
use ensure_clean rather than explicit os.remove #34384 (#34385)
1 parent 4594c2a commit a470a63

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

pandas/tests/io/excel/test_openpyxl.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
import numpy as np
42
import pytest
53

@@ -107,17 +105,15 @@ def test_write_append_mode(ext, mode, expected):
107105
assert wb2.worksheets[index]["A1"].value == cell_value
108106

109107

110-
def test_to_excel_with_openpyxl_engine(ext, tmpdir):
108+
def test_to_excel_with_openpyxl_engine(ext):
111109
# GH 29854
112-
df1 = DataFrame({"A": np.linspace(1, 10, 10)})
113-
df2 = DataFrame({"B": np.linspace(1, 20, 10)})
114-
df = pd.concat([df1, df2], axis=1)
115-
styled = df.style.applymap(
116-
lambda val: "color: %s" % ("red" if val < 0 else "black")
117-
).highlight_max()
118-
119-
filename = tmpdir / "styled.xlsx"
120-
styled.to_excel(filename, engine="openpyxl")
121-
122-
assert filename.exists()
123-
os.remove(filename)
110+
with tm.ensure_clean(ext) as filename:
111+
112+
df1 = DataFrame({"A": np.linspace(1, 10, 10)})
113+
df2 = DataFrame({"B": np.linspace(1, 20, 10)})
114+
df = pd.concat([df1, df2], axis=1)
115+
styled = df.style.applymap(
116+
lambda val: "color: %s" % ("red" if val < 0 else "black")
117+
).highlight_max()
118+
119+
styled.to_excel(filename, engine="openpyxl")

pandas/tests/io/pytables/test_store.py

+15-32
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
import pandas._testing as tm
3737
from pandas.tests.io.pytables.common import (
3838
_maybe_remove,
39-
create_tempfile,
4039
ensure_clean_path,
4140
ensure_clean_store,
4241
safe_close,
43-
safe_remove,
4442
tables,
4543
)
4644

@@ -80,33 +78,25 @@ def test_format_kwarg_in_constructor(self, setup_path):
8078

8179
msg = "format is not a defined argument for HDFStore"
8280

83-
with ensure_clean_path(setup_path) as path:
81+
with tm.ensure_clean(setup_path) as path:
8482
with pytest.raises(ValueError, match=msg):
8583
HDFStore(path, format="table")
8684

8785
def test_context(self, setup_path):
88-
path = create_tempfile(setup_path)
89-
try:
90-
with HDFStore(path) as tbl:
91-
raise ValueError("blah")
92-
except ValueError:
93-
pass
94-
finally:
95-
safe_remove(path)
96-
97-
try:
86+
with tm.ensure_clean(setup_path) as path:
87+
try:
88+
with HDFStore(path) as tbl:
89+
raise ValueError("blah")
90+
except ValueError:
91+
pass
92+
with tm.ensure_clean(setup_path) as path:
9893
with HDFStore(path) as tbl:
9994
tbl["a"] = tm.makeDataFrame()
100-
101-
with HDFStore(path) as tbl:
10295
assert len(tbl) == 1
10396
assert type(tbl["a"]) == DataFrame
104-
finally:
105-
safe_remove(path)
10697

10798
def test_conv_read_write(self, setup_path):
108-
path = create_tempfile(setup_path)
109-
try:
99+
with tm.ensure_clean() as path:
110100

111101
def roundtrip(key, obj, **kwargs):
112102
obj.to_hdf(path, key, **kwargs)
@@ -127,9 +117,6 @@ def roundtrip(key, obj, **kwargs):
127117
result = read_hdf(path, "table", where=["index>2"])
128118
tm.assert_frame_equal(df[df.index > 2], result)
129119

130-
finally:
131-
safe_remove(path)
132-
133120
def test_long_strings(self, setup_path):
134121

135122
# GH6166
@@ -605,7 +592,7 @@ def test_reopen_handle(self, setup_path):
605592

606593
def test_open_args(self, setup_path):
607594

608-
with ensure_clean_path(setup_path) as path:
595+
with tm.ensure_clean(setup_path) as path:
609596

610597
df = tm.makeDataFrame()
611598

@@ -621,8 +608,8 @@ def test_open_args(self, setup_path):
621608

622609
store.close()
623610

624-
# the file should not have actually been written
625-
assert not os.path.exists(path)
611+
# the file should not have actually been written
612+
assert not os.path.exists(path)
626613

627614
def test_flush(self, setup_path):
628615

@@ -4194,7 +4181,6 @@ def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs):
41944181
import tempfile
41954182

41964183
fd, new_f = tempfile.mkstemp()
4197-
41984184
tstore = store.copy(
41994185
new_f, keys=keys, propindexes=propindexes, **kwargs
42004186
)
@@ -4225,20 +4211,17 @@ def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs):
42254211
os.close(fd)
42264212
except (OSError, ValueError):
42274213
pass
4228-
safe_remove(new_f)
4214+
os.remove(new_f)
42294215

42304216
# new table
42314217
df = tm.makeDataFrame()
42324218

4233-
try:
4234-
path = create_tempfile(setup_path)
4219+
with tm.ensure_clean() as path:
42354220
st = HDFStore(path)
42364221
st.append("df", df, data_columns=["A"])
42374222
st.close()
42384223
do_copy(f=path)
42394224
do_copy(f=path, propindexes=False)
4240-
finally:
4241-
safe_remove(path)
42424225

42434226
def test_store_datetime_fractional_secs(self, setup_path):
42444227

@@ -4677,7 +4660,7 @@ def test_read_hdf_generic_buffer_errors(self):
46774660

46784661
def test_invalid_complib(self, setup_path):
46794662
df = DataFrame(np.random.rand(4, 5), index=list("abcd"), columns=list("ABCDE"))
4680-
with ensure_clean_path(setup_path) as path:
4663+
with tm.ensure_clean(setup_path) as path:
46814664
with pytest.raises(ValueError):
46824665
df.to_hdf(path, "df", complib="foolib")
46834666

0 commit comments

Comments
 (0)