Why Use Os.Path.Join Over String Concatenation

Platform independent path concatenation using / , \?

You want to use os.path.join() for this.

The strength of using this rather than string concatenation etc is that it is aware of the various OS specific issues, such as path separators. Examples:

import os

Under Windows 7:

base_dir = r'c:\bla\bing'
filename = r'data.txt'

os.path.join(base_dir, filename)
'c:\\bla\\bing\\data.txt'

Under Linux:

base_dir = '/bla/bing'
filename = 'data.txt'

os.path.join(base_dir, filename)
'/bla/bing/data.txt'

The os module contains many useful methods for directory, path manipulation and finding out OS specific information, such as the separator used in paths via os.sep

Differences between use of os.path.join and os.sep concatenation

It's right there in the documentation:

os.path.join(path1[, path2[, ...]])

Join one or more path components intelligently. If any component is an absolute path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away, and joining continues. The return value is the concatenation of path1, and optionally path2, etc., with exactly one directory separator (os.sep) following each non-empty part except the last. (This means that an empty last part will result in a path that ends with a separator.) Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

os.path.join does much more:

>>> os.path.join("/home/", "/home/foo")
'/home/foo'
>>> "/home/" + os.sep + "/home/foo"
'/home///home/foo'

You will never have a situation where os.path.join is the bottleneck of your program, so use it, it's much more readable too.

Concatenate path and filename

Backslash character (\) has to be escaped in string literals.

  • This is wrong: '\'
  • This is correct: '\\' - this is a string containing one backslash

Therefore, this is wrong:

'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'

There is a trick!

String literals prefixed by r are meant for easier writing of regular expressions. One of their features is that backslash characters do not have to be escaped. So, this would be OK:

r'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'

However, that wont work for a string ending in backslash:

  • r'\' - this is a syntax error

So, this is also wrong:

r'C:\Users\A\Desktop\Repo\'

So, I would do the following:

import os
import subprocess

soffice = 'C:\\Program Files (x86)\\LibreOffice 5\\program\\soffice.exe'
outdir = 'C:\\Users\\A\\Desktop\\Repo\\'
full_path = os.path.join(outdir, filename)

subprocess.call([soffice,
'--headless',
'--convert-to', 'pdf',
'--outdir', outdir,
full_path])

Control order of pathlib and string concatenation

Just use parentheses surrounding your string contatenation:

path2newfile = dir / (constantString + changingString)


Related Topics



Leave a reply



Submit