question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SIM906] Merge nested os.path.join calls

See original GitHub issue

Explanation

Explain briefly why you think this makes the code simpler.

  • This is a common but fun one I have noticed. I have noticed this pattern occur a lot and I believe in most cases it can be simplified to a single function call. I guess this would be kinda of similar to the isinstance checks where people don’t realize the function can consume multiple args.

Example

a = "/a"
b = "b"
c = "c"
# Bad
os.path.join(a,os.path.join(b,c))

# Good
os.path.join(a,b,c)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Skylion007commented, Feb 16, 2022

@MartinThoma True, but there can be performance issues with using Pathlib. For instance, I think the black formatter removed / reworked a lot of their Pathlib usage back to os.path in their library due to performance issues.

0reactions
MartinThomacommented, Feb 20, 2022
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.txt`
Module(
    body=[
        Expr(
            value=Call(
                func=Attribute(
                    value=Attribute(
                        value=Name(id='os', ctx=Load()),
                        attr='path',
                        ctx=Load(),
                    ),
                    attr='join',
                    ctx=Load(),
                ),
                args=[
                    Name(id='a', ctx=Load()),
                    Call(
                        func=Attribute(
                            value=Attribute(
                                value=Name(id='os', ctx=Load()),
                                attr='path',
                                ctx=Load(),
                            ),
                            attr='join',
                            ctx=Load(),
                        ),
                        args=[
                            Name(id='b', ctx=Load()),
                            Name(id='c', ctx=Load()),
                        ],
                        keywords=[],
                    ),
                ],
                keywords=[],
            ),
        ),
    ],
    type_ignores=[],
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to combine more than 2 file paths at once?
I want to combine multiple file maths together, but I end up with nested stuff like os.path.join(os.path.join(“assets”, “imgs”), ...
Read more >
os.path — Common pathname manipulations — Python 3.11 ...
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one...
Read more >
Python os.path.join() method [Practical Examples]
This means that we can merge multiple parts of a path into one using the os.path.join method instead of hard-coding every pathname manually....
Read more >
Python | os.path.join() method - GeeksforGeeks
path module is sub-module of OS module in Python used for common pathname manipulation. os.path.join() method in Python join one or more path ......
Read more >
Why you should be using pathlib - Trey Hunner
from os.path import abspath, dirname, join as joinpath BASE_DIR ... The os.path module requires function nesting, but the pathlib modules' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found