Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.path.join
#1
This has me confused as to why it does not work:
import os

PARENT_DIR = '/Volumes/Sidekick/SEER/Trip'
SOURCE_DIR = '/data/source'

# first do as concatenation and show result

OUT_DIR = PARENT_DIR + SOURCE_DIR
Print(OUT_DIR) 
Output:
>> /Volumes/Sidekick/SEER/Trip/data/source <<
# Now, do using os.path.join and show result
OUT1_DIR = os.path.join(PARENT_DIR, SOURCE_DIR)
Print(OUT1_DIR)
Output:
>> /data/source <<
Anyone have any ideas on what is happening?

TNX
Larz60+ write Nov-07-2020, 08:49 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed this time. Please use going forward.
Reply
#2
Quote:Help on function join in module posixpath:

join(a, *p)
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded.
An empty last part will result in a path that
ends with a separator.

Your second path starts with a separator, so it is considered an absolute path. Therefore the first path is discarded.

>>> os.path.join('/Volumes/Sidekick/SEER/Trip', '/data/source')
'/data/source'
>>> os.path.join('/Volumes/Sidekick/SEER/Trip', 'data/source')
'/Volumes/Sidekick/SEER/Trip/data/source'
Reply
#3
(Nov-07-2020, 06:14 PM)bowlofred Wrote:
Quote:Help on function join in module posixpath:

join(a, *p)
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded.
An empty last part will result in a path that
ends with a separator.

Your second path starts with a separator, so it is considered an absolute path. Therefore the first path is discarded.

>>> os.path.join('/Volumes/Sidekick/SEER/Trip', '/data/source')
'/data/source'
>>> os.path.join('/Volumes/Sidekick/SEER/Trip', 'data/source')
'/Volumes/Sidekick/SEER/Trip/data/source'

Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  os.path.join - errors out tester_V 4 483 Nov-29-2020, 08:57 AM
Last Post: DeaD_EyE
  The difference between os.path.join( and os.sep.join( Pedroski55 2 808 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 912 Feb-14-2019, 08:34 PM
Last Post: woooee
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 2,722 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

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