The os.path.join(a, b)
method will generate a string ending without '/' no matter it is a file or directory. Now, is there any way (or any other os.path
method) to get a '/' automatically for the case of the directory?
path. join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator ('/') following each non-empty part except the last path component.
Answer. Your path (i.e. the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since path is a list, you can use the append method to add new directories to the path.
os. path. join(path, '') will add the trailing slash if it's not already there.
os. path. join combines path names into one complete path. This means that you can merge multiple parts of a path into one, instead of hard-coding every path name manually.
There is no such function in os.path
. It's easy to code up yourself, though:
if os.path.isdir(path):
path = os.path.join(path, "")
This will add a /
if there isn't already one at the end of path
in case it points to a directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With