Python os
module provides the path
submodule with the join()
method. The os.path.join()
method is used to join multiple paths and file name as a string operation. In this tutorial we examine the o.spath.join() method with different examples like join path and file name or join multiple paths with the file name. The join() method simply returns the joined path and file name information.
Join Path and File Name
We can use the os.path.join() method in order to join path and file name string. In the followig example we use the “/home/ismail/” as the path string and “db.txt” as file name string.
import os
s = os.path.join("/home/ismail","db.txt")
Alternatively we can use the path information as variable like below.
import os
path="/home/ismail"
s = os.path.join(path,"db.txt")
We can use both the path and file name as variables.
import os
path="/home/ismail"
file="db.txt"
s = os.path.join(path,file)
Join Multiple Paths and File Name
We can use os.path.join() method in order to join multiple paths with file name.
import os
s = os.path.join("/home/ismail","backup","db.txt")