【问题标题】:Running a Python program from the command line using only the program name仅使用程序名称从命令行运行 Python 程序
【发布时间】:2018-01-29 20:16:04
【问题描述】:

我被要求制作一个解析令牌的 python 程序。

用法如下:

$ cat input.txt | program "some text %{0} and %{1S3}" > output.txt

但我能做到的最好的是:

$ cat input.txt | py program.py "some text %{0} and %{1S3}" > output.txt

或者如果我使脚本可执行,删除文件扩展名,我在当前目录中

$ cat input.txt | ./program "some text %{0} and %{1S3}" > output.txt

有没有办法让我使用第一个示例的执行方式和 python 脚本?理想情况下,我还可以在任何地方使用它,而不是在指向包含程序的目录时使用它。

编辑:我试过这个:

这是我尝试过的--

$ cd projects/token_parser/
$ ln -s grap /usr/local/bin/grap
ln: failed to create symbolic link '/usr/local/bin/grap': Permission denied
$ sudo ln -s grap /usr/local/bin/grap 
[sudo] password for fosssaintdross: 
$ grap
bash: grap: command not found

【问题讨论】:

  • 你为什么不直接从你的 python 脚本中读写文件呢?
  • 这是一次面试的代码试镜,所以我试图尽可能地匹配他们的规范。
  • 提示:PATH...
  • this答案
  • 我不会担心对以cat input.txt | program ... 为例的人来说过于挑剔。

标签: python linux bash scripting


【解决方案1】:

您需要确保包含program.py 的位置在PATH 环境变量中。或者,您可以从列表中已有的路径链接到它,例如:

ln -s /path/to/program.py /usr/local/bin/program

【讨论】:

  • 如果您实际上对/usr/local 具有写入权限,那么进行正确安装通常是一个更好的主意,而不是向/usr/local/bin 中添加任意符号链接。
  • 我添加了更多信息。
  • 尝试链接到完整路径
  • 这些链接在会话之间是否持续存在?还是暂时的?
  • 它作为你的文件系统是持久的。因此,对于常规的笔记本电脑/台式机安装,答案是肯定的。
猜你喜欢
  • 1970-01-01
  • 2020-05-09
  • 2020-04-26
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多