Command Line – Why Use ‘/home/username/’ Instead of ‘~’ for File Paths

bashcommand linepython

I can use ~ instead of /home/username/ to point to a file path when, for example, unzipping a .zip file.

However, today when I followed the same way to run a RNN example in terminal, tensorflow.python.framework.errors_impl.NotFoundError was thrown.

$ python ptb_word_lm.py --data_path=~/anaconda2/lib/python2.7/site-packages/tensorflow/models-master/tutorials/rnn/simple-examples/data/ --model=small 
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally
Traceback (most recent call last):
  File "ptb_word_lm.py", line 374, in <module>
    tf.app.run()
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "ptb_word_lm.py", line 321, in main
    raw_data = reader.ptb_raw_data(FLAGS.data_path)
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/models-master/tutorials/rnn/ptb/reader.py", line 73, in ptb_raw_data
    word_to_id = _build_vocab(train_path)
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/models-master/tutorials/rnn/ptb/reader.py", line 34, in _build_vocab
    data = _read_words(filename)
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/models-master/tutorials/rnn/ptb/reader.py", line 30, in _read_words
    return f.read().decode("utf-8").replace("\n", "<eos>").split()
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 106, in read
    self._preread_check()
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 73, in _preread_check
    compat.as_bytes(self.__name), 1024 * 512, status)
  File "/home/hok/anaconda2/lib/python2.7/contextlib.py", line 24, in __exit__
    self.gen.next()
  File "/home/hok/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: ~/anaconda2/lib/python2.7/site-packages/tensorflow/models-master/tutorials/rnn/simple-examples/data/ptb.train.txt

Then I replaced ~ with /home/username/, and it worked properly.

Why couldn't I use ~ instead of /home/username/ to point to the file path when runing a RNN example?

Could you tell me in detail?

Best Answer

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.

Related Question