Search code examples
pythonstringpytestpathlib

Python pathlib.unlink: 'str' object has no attribute '_accessor'


I am trying to use Path.unlink to remove test files in a pytest fixture, but I keep getting this error on teardown.

The fixture teardown code:

    yield
    try:
        for i in range(2):
            file = db_helper(f'testfile{i}')
            Path.unlink(file)
    finally:
        file = db_helper('testfile')
        Path.unlink(file)

db_helper:

def db_helper(filename):
    """Helper function for generate_db and retrieve_db"""
    if name == "posix":
        home = getenv('HOME')
        path = Path(f'{home}/.local/apikeychain/')
    elif name == "nt":
        appdata = getenv('APPDATA')
        path = Path(str(f'{appdata}/apikeychain/'))
    else:
        raise NotImplementedError

    path.mkdir(parents=True, exist_ok=True)
    return f'{path}/{filename}.db'

The output:

FTraceback (most recent call last):
  File "/home/ctrenthem/api-keychain/tests/conftest.py", line 17, in tmp_keychain
    Path.unlink(file)
  File "/usr/lib/python3.9/pathlib.py", line 1344, in unlink
    self._accessor.unlink(self)
AttributeError: 'str' object has no attribute '_accessor'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
<snip>
  File "/home/ctrenthem/.cache/pypoetry/virtualenvs/api-keychain-R4MwVzYJ-py3.9/lib/python3.9/site-packages/_pytest/fixtures.py", line 941, in _teardown_yield_fixture
    next(it)
  File "/home/ctrenthem/api-keychain/tests/conftest.py", line 20, in tmp_keychain
    Path.unlink(file)
  File "/usr/lib/python3.9/pathlib.py", line 1344, in unlink
    self._accessor.unlink(self)
AttributeError: 'str' object has no attribute '_accessor'

I've tried converting the string using Path(file), Path.path(file), and a couple other Path functions, but each of them gives a similar AttributeError. This code needs to run successfully for the tests to clean up properly, because part of the code being tested raises errors if the file already exists.

Also, as demonstrated in the db_helper function, I cannot just hardcode the files because the db file is generated in a particular location depending on the user's system, and even if I did hardcode those paths in, I would still be stuck with the fact that my file is still going to be a string object


Solution

  • Code

    f'{path}/{filename}.db' 
    

    creates string.

    You should use

    path / f'{filename}.db'
    

    to get Path object.

    And then you can do

    file = db_helper('testfile')
    file.unlink()
    

    BTW:

    You can also use

        home = Path.home()
        path = home / '.local/apikeychain'
    

    or shorter

        path = Path.home() / '.local/apikeychain'
    

    to make code simpler.