Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';Python需要单击脚本才能访问它';谁有自己的模块目录?_Python_Command Line Interface_Python Click - Fatal编程技术网

什么';Python需要单击脚本才能访问它';谁有自己的模块目录?

什么';Python需要单击脚本才能访问它';谁有自己的模块目录?,python,command-line-interface,python-click,Python,Command Line Interface,Python Click,如何在主目录中的任意位置使用Python单击脚本,并在模块目录中配置脚本访问权限 假设我有一个名为ClickDir的小脚本,它位于我的主目录~/bin中。下面是一个简单脚本的目录树: . ├── ClickDir │   ├── config │   │   └── helloworld_input.txt │   ├── __init__.py │   └── scripts │   ├── clickscript.py │   └── __init__.py ├── Hel

如何在主目录中的任意位置使用Python单击脚本,并在模块目录中配置脚本访问权限

假设我有一个名为
ClickDir
的小脚本,它位于我的主目录
~/bin
中。下面是一个简单脚本的目录树:

.
├── ClickDir
│   ├── config
│   │   └── helloworld_input.txt
│   ├── __init__.py
│   └── scripts
│       ├── clickscript.py
│       └──  __init__.py
├── HelloWorld_output.txt
└── setup.py
这是一个非常无用的剧本。它读取
config
中的文件,并将此文本与您选择的输入文本文件连接起来。最后,它将
HelloWorld\u output.txt
写入与输入文件相同的目录

用法:单击目录[选项]输入

我可以在任何地方调用Click函数,但是Click也可以相对于该目录查找
config/helloworld\u input.txt
。因此,脚本无法找到配置输入

FileNotFoundError: [Errno 2] No such file or directory: '../config/helloworld_input.txt'
我需要在Linux和Windows中应用此功能(Win 10上的Anaconda)

更新:

import click
import os
dir_path = os.path.dirname(os.path.realpath("config/helloworld_input"))

#@cli.command()
@click.command()
@click.argument('input', type=click.File('rb'))
@click.option('--saveas', default='HelloWorld.txt', type=click.File('wb'))
def inout(input, saveas):
    with open(dir_path + "/helloworld_input") as conf:
        print(conf.read())
    while True:
        chunk = input.read(1024)
        if not chunk:
            break
        saveas.write(chunk)
我的脚本位于此处:
/home/xtian/Documents.x/Projects/CliTools.d/ClickDir/scripts

我备份了几个目录,并从项目中运行了脚本,但不幸的是,下面描述的解决方案只是从调用目录中调用--与之前相同:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/home/xtian/Documents.x/Projects/config/helloworld\u input'
您可以使用文件的绝对路径而不是相对路径。例如:
~/bin/ClickDir/config/helloworld\u input.txt

您可以使用文件的绝对路径而不是相对路径。例如:
~/bin/ClickDir/config/helloworld\u input.txt

您可以使用
\uuuu file\uuu
伪变量来查找脚本所在的目录

dir\u path=os.path.dirname(os.path.realpath(\u文件\u))


然后,您可以使用
os.path.join
来组成数据文件的完整路径

您可以使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

dir\u path=os.path.dirname(os.path.realpath(\u文件\u))


然后,您可以使用
os.path.join
组合数据文件的完整路径

您需要在
clickscript.py
中显示代码。还有,这和水蟒有什么关系?我删除了Anaconda标记,但是如果我丢失了一些连接,请重新添加它。您需要在
clickscript.py
中显示代码。还有,这和水蟒有什么关系?我删除了Anaconda标记,但是如果有连接未命中,请重新添加它。第3行应该是
dir\u path=os.path.dirname(os.path.realpath(\uu file\uu))
方法中的第一个函数应该是带有open(os.path.join(dir\u path,“helloworld\u input”))的
,如果我了解如何使用它,它会有帮助。(>\这是一个变量,但在模块文件中不同。它的值是加载模块的文件名的完整路径。您需要使用realpath来处理一些奇怪的情况。我不确定如果您不是从文件中加载,而是从“egg”或其他奇怪的空间中加载,它会设置为什么。在您的情况下,它将始终设置正确ly.line 3应该是
dir\u path=os.path.dirname(os.path.realpath(\uu文件\uuu))
方法中的第一个函数应该是
,带有open(os.path.join(dir\u path,“helloworld\u input”))作为conf
帮助我理解如何使用它。(>\这是一个变量,但在模块文件中不同。它的值是加载模块的文件名的完整路径。您需要使用realpath来处理一些奇怪的情况。我不确定如果您不是从文件中加载,而是从“egg”或其他奇怪的空间中加载,它会设置为什么。在您的情况下,它将始终设置正确很好。