第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 popen 命令行參數(shù)包含單引號和雙引號?

如何使用 popen 命令行參數(shù)包含單引號和雙引號?

慕絲7291255 2022-10-06 16:55:01
我想在 python3中運行以下jq命令。subprocess.Popen()$ jq  'INDEX(.images[]; .id) as $imgs | {    "filename_with_label":[         .annotations[]        | select(.attributes.type=="letter" )        | $imgs[.image_id] + {label:.text}        | {id:.id} + {filename:.file_name} + {label:.label}     ]   }' image_data_annotation.json > image_data_annotation_with_label.json請注意,第一個命令行參數(shù)包含點、美元符號、單引號內的雙引號。僅供參考,jq是用于處理 json 文件的 JSON 處理器實用程序。我編寫了以下 python3 腳本,用于使用jq實用程序自動處理 JSON 文件。#!python3# file name: letter_image_tool.pyimport os, subprocess"""command line example to automate$ jq  'INDEX(.images[]; .id) as $imgs | {    "filename_with_label":[         .annotations[]        | select(.attributes.type=="letter" )        | $imgs[.image_id] + {label:.text}        | {id:.id} + {filename:.file_name} + {label:.label}     ]   }' image_data_annotation.json > image_data_annotation_with_label.json"""# define first command line argumentjq_filter='\'INDEX(.images[]; .id) as $imgs | { "filename_with_label" : [ .annotations[] | select(.attributes.type=="letter" ) | $imgs[.image_id] + {label:.text} | {id:.id} + {filename:.file_name} + {label:.label} ] }\''input_json_files= [ "image_data_annotation.json"]output_json_files= []for input_json in input_json_files:    print("Processing %s" %(input_json))    filename, ext = os.path.splitext(input_json)    output_json = filename + "_with_label" + ext    output_json_files.append(output_json)    print("output file is : %s" %(output_json))    #jq_command ='jq' + " " +  jq_filter, input_json + ' > ' +  output_json    jq_command =['jq', jq_filter,  input_json + ' > ' +  output_json]    print(jq_command)    subprocess.Popen(jq_command, shell=True)第一個參數(shù)應該像上面的片段一樣用單引號括起來,但我的腳本不處理它。我認為主要問題與第一個命令行參數(shù)(jq_filter在上面的 python 腳本中)中使用的點、美元符號、單引號和雙引號有關。但是我不知道如何處理這種與bash相關的復雜元字符。我應該怎么做才能解決以上問題?
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經(jīng)驗 獲得超3個贊

您需要單引號的原因是為了防止 shell 對您的參數(shù)進行任何擴展。這是一個問題,只有在使用shell=True. 如果未設置,shell 將永遠不會觸及您的參數(shù),也無需“保護”它們。


然而,shell 也負責stdout重定向(即[... '>', output_json])。不使用 shell,需要在 Python 代碼中處理重定向。然而,這就像將參數(shù)添加stdout=...到Popen.


總而言之,這意味著您的代碼可以重寫為


import os

import subprocess


# Still define first command line argument with triple quotes for readability

# Note that there are no single quotes though

jq_filter = """INDEX(.images[]; .id) as $imgs | {

       "filename_with_label" : [

        .annotations[]

       | select(.attributes.type=="letter" )

       | $imgs[.image_id] + {label:.text}

       | {id:.id} + {filename:.file_name} + {label:.label} ] }"""


input_json_files = ["image_data_annotation.json"]

output_json_files = []


for input_json in input_json_files:

    print("Processing %s" % (input_json))

    filename, ext = os.path.splitext(input_json)

    output_json = filename + "_with_label" + ext

    output_json_files.append(output_json)

    print("output file is : %s" % (output_json))


    # Keep command as list, since this is what we need when NOT using shell=True

    # Note also that the redirect and the output file are not parts of the argument list

    jq_command = ['jq', jq_filter,  input_json]


    # shell keyword argument should NOT be set True

    # Instead redirect stdout to an out_file

    # (We must open the file for writing before redirecting)

    with open(output_json, "w") as out_file:

        subprocess.Popen(jq_command, stdout=out_file)

通常建議不要使用shell=True,因為這會打開另一個針對代碼的攻擊向量,因為注入攻擊可以完全訪問 shell。此外,不使用 shell 的另一個小好處是,它將減少創(chuàng)建的子進程的數(shù)量,因為不需要額外的 shell 進程。


查看完整回答
反對 回復 2022-10-06
  • 1 回答
  • 0 關注
  • 275 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號