如何使用命令提示符添加一行文本并保存?

时间:2018-10-01 02:10:49

标签: batch-file cmd

即时通讯方式使用CMD在文本文件中添加一行。

  

以下是原始文件。

{"jobStorePath":"JobStore","spoolerId":"68f8b4d1-e9c2-40ca-93bd-b10e9c233f7f","uiId":"68f8b4d1-e9c2-40ca-93bd-b10e9c233f7f-ClientUI","defaultPaperSize":"A4"}

,我想补充一下 该行中的“ updateLocations”:“ false”。

  

结果

{"jobStorePath":"JobStore","spoolerId":"68f8b4d1-e9c2-40ca-93bd-b10e9c233f7f","uiId":"68f8b4d1-e9c2-40ca-93bd-b10e9c233f7f-ClientUI","defaultPaperSize":"A4","updateLocations": "false"}

1 个答案:

答案 0 :(得分:0)

使用python脚本完成可从命令行调用的工作。

#!/usr/bin/env python

import os, sys
import json

if len(sys.argv) != 2:
    print('invalid input arguments.')
    print('python3 insert_update_locations_false.py [file_name]')
    exit()

file_name = sys.argv[1]

def main():
    print_job = read_file_in()
    print_job['updateLocations'] = False
    rewrite_file(print_job)


def read_file_in():
    with open(file_name, 'r') as f:
        jsonObj = json.load(f)
        return jsonObj


def rewrite_file(jsonObj):
    with open(file_name, 'w') as f:
        json.dump(jsonObj, f)


main()

然后您可以从命令行中按如下方式调用它:

python3 insert_update_locations_false.py job1.txt
相关问题