Why couldn't I use ' ~ ' instead of ' /home/username/ ' when giving the file path
You need to understand that ~
is normally expanded by the shell; the programs you call never see it, they see the full pathname as inserted by bash. But this only happens when the tilde is at the start of an argument (and is not quoted).
If the Python program you are running uses a module like getopt
to parse its commandline, you can give the argument of the --data-path
option as a separate "word" to allow tilde expansion:
$ python ptb_word_lm.py --data_path ~/anaconda2/lib/python2.7/...
In your own code, you can use getopt
or argparse
for argument processing, and could also manually expand tildes as @JacobVlijm's answer suggested.
PS. The tilde is also expanded at the start of a shell variable assignment expression like DIRNAME=~/anaconda2
; although the tilde in your question also follows an equals sign, this usage doesn't have special meaning for the shell (it's just something passed to a program) and doesn't trigger expansion.
Tilde expansion in python
The answer is short & simple:
python does not expand ~
unless you use:
import os
os.path.expanduser('~/your_directory')
See also here:
os.path.expanduser(path)
On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user‘s home directory.On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.
Tilde expansion is only done in a few contexts that vary slightly between shells.
While it is performed in:
var=~
Or
export var=~
in some shells. It's not in
echo var=~
env var=~ cmd
./configure --prefix=~
in POSIX shells.
It is in bash
though when not in POSIX conformance mode (like when called as sh
, or when POSIXLY_CORRECT
is in the environment):
$ bash -c 'echo a=~'
a=/home/stephane
$ POSIXLY_CORRECT= bash -c 'echo a=~'
a=~
$ SHELLOPTS=posix bash -c 'echo a=~'
a=~
$ (exec -a sh bash -c 'echo a=~')
a=~
However that's only when what's on the left of the =
is shaped like an unquoted valid variable name, so while it would be expanded in cmd prefix=~
, it would not be in cmd --prefix=~
(as --prefix
is not a valid variable name) nor in cmd "p"refix=~
(because of that quoted p
) nor in var=prefix; cmd $var=~
.
In zsh
, you can set the magic_equal_subst
option for ~
to be expanded after any unquoted =
.
$ zsh -c 'echo a=~'
a=~
$ zsh -o magic_equal_subst -c 'echo a=~'
a=/home/stephane
$ zsh -o magic_equal_subst -c 'echo --a=~'
--a=/home/stephane
In the case of ~
(as opposed to ~user
), you can just use $HOME
instead:
cmd --whatever="$HOME/whatever"
~
expands to the value of $HOME
. If $HOME
is not set, behaviour varies between shells. Some shells query the user database. If you want to take that into account, you could do (and that's also what you would have to do for ~user
):
dir=~ # or dir=~user
cmd --whatever="$dir/whatever"
In any case, in shells other than zsh
remember you need to quote variable expansions!