Skip to content

Refactor to use pathlib #210

Description

@csatt

This Issue was brought to my attention by the "refurb" tool. Among many others, refurb makes the following suggestions:

[FURB104]: Replace os.getcwd() with Path.cwd()
[FURB141]: Replace os.path.exists(x) with Path(x).exists()
[FURB144]: Replace os.remove(x) with Path(x).unlink()
[FURB150]: Replace os.makedirs(x) with Path(x).mkdir(parents=True)

"Path" is referring to the Path class of the pathlib module. The pathlib module was introduced in Python 3.4, and is a more "modern" way to deal with the filesystem using objects instead of strings. There are many articles about why new code should use pathlib instead of the os module, and many advocate refactoring existing code. There's a pretty good counterargument, which is, "if it ain't broke, don't fix it." But I have generally tended to try to incrementally improve the IV Swinger 2 code toward adhering to Python coding best practices, where feasible.

The four cases that refurb calls out are just scratching the surface. There are many other cases of os module functions that can and should be refactored to use pathlib. This Issue will track these changes.

I will file another Issue later to address all of the other refurb suggestions not related to pathlib.

Metadata

Metadata

Assignees

Labels

cosmeticCode cleanup, etc.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions