我对python很陌生,我正在尝试读取一个JSON文件,目前我只需将其写入一个新文件,而不需要进行任何更改。我一直试图使用python包Click来完成这个操作,但始终会遇到错误。
我相信这是比较基本的,但任何帮助都将不胜感激。下面是我尝试过的代码的最新版本。
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()发布于 2021-10-16 12:14:32
像这样的东西可以让您知道如何使用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()https://stackoverflow.com/questions/69594900
复制相似问题