使用 Python 单击以读取 JSON 文件

Using Python Click to read a JSON file

我是 python 的新手,我正在尝试读取 JSON 文件,现在我可以不做任何更改地写入一个新文件。我一直在尝试使用 python 包单击以执行此操作,但一直 运行 出错。

我确信这是相对基础的,但如有任何帮助,我们将不胜感激。我试过的最新版本的代码如下。

 import json
 import os
 import click


 def dazprops():
     """Read Daz3D property File"""
     path = click.prompt('Please specify a location for the Daz3D Properties File')
     path = os.path.realpath(path)
     #dir_name = os.path.dirname(path)
     print(path)
     dazpropsf = open('path')
     print(dazpropsf)


 if __name__ == '__main__':
     dazprops()

类似这样的内容可以让您了解如何使用 click:

实现该目标
import click

def read_file(fin):
    content = None
    with open(fin, "r") as f_in:
        content = f_in.read()
    return content

def write_file(fout, content):
    try:
        print("Writing file...")
        with open(fout, "w") as f_out:
            f_out.write(content)
        print(f"File created: {fout}")
    except IOError as e:
        print(f"Couldn't write a file at {fout}. Error: {e}")

@click.command()
@click.argument('fin', type=click.Path(exists=True))
@click.argument('fout', type=click.Path())
def init(fin, fout):
    """
    FINT is an input filepath
    
    FOUT is an output filepath
    """
    content = read_file(fin)
    if content:
        write_file(fout, content)
                
if __name__ == "__main__":
    init()