diff --git a/pandas/_testing/contexts.py b/pandas/_testing/contexts.py index 5a77c06d65d07..547ec9db20994 100644 --- a/pandas/_testing/contexts.py +++ b/pandas/_testing/contexts.py @@ -3,14 +3,13 @@ from contextlib import contextmanager import os from pathlib import Path -import random from shutil import rmtree -import string import tempfile from typing import ( IO, Any, ) +import uuid import numpy as np @@ -107,9 +106,7 @@ def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any): if filename is None: filename = "" - filename = ( - "".join(random.choices(string.ascii_letters + string.digits, k=30)) + filename - ) + filename = str(uuid.uuid4()) + filename path = folder / filename path.touch() diff --git a/pandas/tests/io/parser/common/test_file_buffer_url.py b/pandas/tests/io/parser/common/test_file_buffer_url.py index b9ddec0a37c11..fce1d1260b3fe 100644 --- a/pandas/tests/io/parser/common/test_file_buffer_url.py +++ b/pandas/tests/io/parser/common/test_file_buffer_url.py @@ -9,6 +9,7 @@ import os import platform from urllib.error import URLError +import uuid import pytest @@ -87,7 +88,7 @@ def test_nonexistent_path(all_parsers): # gh-14086: raise more helpful FileNotFoundError # GH#29233 "File foo" instead of "File b'foo'" parser = all_parsers - path = f"{tm.rands(10)}.csv" + path = f"{uuid.uuid4()}.csv" msg = r"\[Errno 2\]" with pytest.raises(FileNotFoundError, match=msg) as e: @@ -255,7 +256,7 @@ def test_internal_eof_byte_to_file(all_parsers): parser = all_parsers data = b'c1,c2\r\n"test \x1a test", test\r\n' expected = DataFrame([["test \x1a test", " test"]], columns=["c1", "c2"]) - path = f"__{tm.rands(10)}__.csv" + path = f"__{uuid.uuid4()}__.csv" with tm.ensure_clean(path) as path: with open(path, "wb") as f: diff --git a/pandas/tests/io/parser/test_encoding.py b/pandas/tests/io/parser/test_encoding.py index a70c3ee44edb6..c06ac9e76bd7f 100644 --- a/pandas/tests/io/parser/test_encoding.py +++ b/pandas/tests/io/parser/test_encoding.py @@ -2,10 +2,10 @@ Tests encoding functionality during parsing for all of the parsers defined in parsers.py """ - from io import BytesIO import os import tempfile +import uuid import numpy as np import pytest @@ -54,7 +54,7 @@ def test_utf16_bom_skiprows(all_parsers, sep, encoding): 4,5,6""".replace( ",", sep ) - path = f"__{tm.rands(10)}__.csv" + path = f"__{uuid.uuid4()}__.csv" kwargs = {"sep": sep, "skiprows": 2} utf8 = "utf-8" diff --git a/pandas/tests/io/pytables/conftest.py b/pandas/tests/io/pytables/conftest.py index 988f78c5ae843..bbd815b9c6309 100644 --- a/pandas/tests/io/pytables/conftest.py +++ b/pandas/tests/io/pytables/conftest.py @@ -1,3 +1,5 @@ +import uuid + import pytest import pandas._testing as tm @@ -6,7 +8,7 @@ @pytest.fixture def setup_path(): """Fixture for setup path""" - return f"tmp.__{tm.rands(10)}__.h5" + return f"tmp.__{uuid.uuid4()}__.h5" @pytest.fixture(scope="module", autouse=True) diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py index 56fc42aa0e2db..3ad8fd4051b48 100644 --- a/pandas/tests/io/test_pickle.py +++ b/pandas/tests/io/test_pickle.py @@ -21,6 +21,7 @@ from pathlib import Path import pickle import shutil +import uuid from warnings import ( catch_warnings, filterwarnings, @@ -248,7 +249,7 @@ def test_legacy_sparse_warning(datapath, typ): @pytest.fixture def get_random_path(): - return f"__{tm.rands(10)}__.pickle" + return f"__{uuid.uuid4()}__.pickle" class TestCompression: