在给出文件路径时,为什么我不能使用'〜'而不是'/ home / username /'

Modified on: Sat, 09 Jun 2018 11:50:23 +0800

我可以使用~代替/home/username/来指向文件路径,例如,解压缩.zip文件。

然而,今天当我按照相同的方式在终端中运行RNN示例时,抛出了tensorflow.python.framework.errors_impl.NotFoundError

$ 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

然后我用~替换了/home/username/,它运行正常。

为什么我不能在运行RNN示例时使用~而不是/home/username/指向文件路径?

你能详细告诉我吗?

最佳答案

您需要了解~通常由shell扩展;你调用的程序永远不会看到它,它们会看到bash插入的完整路径名。但这只发生在波浪号在参数开始时(并且没有引用)。

如果您运行的Python程序使用类似getopt的模块来解析其命令行,则可以将--data-path选项的参数作为单独的参数提供“字”允许波浪扩展:

$ python ptb_word_lm.py --data_path ~/anaconda2/lib/python2.7/...

在您自己的代码中,您可以使用getoptargparse进行参数处理,也可以手动扩展波浪线,如@ JacobVlijm的回答所示。

PS。代码也在 shell 变量赋值表达式的开头展开,如DIRNAME=~/anaconda2;虽然你问题中的代字号也跟随等号,但这种用法对shell没有特殊意义(它只是传递给程序的东西)并且不会触发扩展。

作者:,alexis

相关问答

添加新评论