Python Forum
The difference between os.path.join( and os.sep.join(
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The difference between os.path.join( and os.sep.join(
#1
What is the difference between these 2?

1. with os.sep.join([dirpath, filename])

for (dirpath, dirnames, filenames) in os.walk(path):
    for filename in filenames:
        if filename.endswith('.jpg'): 
            list_of_files[filename] = os.sep.join([dirpath, filename])
2. os.path.join(dirpath, filename)

for (dirpath, dirnames, filenames) in os.walk(path):
    for filename in filenames:
        if filename.endswith('.jpg'): 
            list_of_files[filename] = os.path.join(dirpath, filename)
Both seem to produce the same result. Is either preferable??
Reply
#2
os.path.join is preferable.

os.sep is simply a character. So os.sep.join is just the normal string join.

>>> type(os.sep)
<class 'str'>
>>> os.sep.join(["foobar", "/foo/baz/", "whatever"])
'foobar//foo/baz//whatever'
os.path.join joins them with some more intelligence. If you have multiple separators, it will take it down to one. If you have a component that is an absolute path (starts with a separator), it will ignore the paths before it, etc.

>>> os.path.join("foobar", "/foo/baz/", "whatever")
'/foo/baz/whatever'
Pedroski55 and buran like this post
Reply
#3
Thanks!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ''.join and start:stop:step notation for lists ringgeest11 1 435 Apr-08-2021, 04:29 PM
Last Post: BashBedlam
  os.path.join - errors out tester_V 4 587 Nov-29-2020, 08:57 AM
Last Post: DeaD_EyE
  os.path.join qmfoam 2 544 Nov-08-2020, 04:03 PM
Last Post: qmfoam
  how does .join work with list and dictionaries gr3yali3n 7 981 Jul-07-2020, 09:36 PM
Last Post: bowlofred
  join string lists redminote4dd 9 995 Jun-11-2020, 07:04 PM
Last Post: Yoriz
  Join table with differnt datype column using python query abhishek6555 1 800 Jan-07-2020, 11:44 PM
Last Post: micseydel
  how to join 2 iterators Skaperen 2 1,036 Sep-11-2019, 07:19 PM
Last Post: Skaperen
  Question on Join() function sduvvuri 2 996 Jun-02-2019, 03:55 PM
Last Post: perfringo
  Threads join() question adam2020 4 1,241 Mar-19-2019, 09:30 PM
Last Post: adam2020
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 978 Feb-14-2019, 08:34 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020