python - Understanding how `os.path.join` works with `os.path.expanduser` -
what line of code mean
datafolder = os.path.join(os.path.expanduser("~"), "data", "books")
does line create folder called datafolder , if can insert files , load file through line
!load getdata.py
from docs:
os.path.expanduser(path)
on unix , windows, return argument initial component of ~ or ~user replaced user‘s home directory.
in unix, home directory represented tilde (~
) sign. using os.path.expanduser
expands tilde actual path:
in [765]: os.path.expanduser("~") out[765]: '/users/coldspeed'
this string, along 'data'
, 'books'
, joined os.path.join
form qualified path:
in [766]: os.path.join(os.path.expanduser("~"), 'data', 'books') out[766]: '/users/coldspeed/data/books'
this convenient way specify home directory without having hardcode it.
Comments
Post a Comment