<?php
$nodes = [];
$_tmp['title'] = '标题1';
$_tmp['titlekey'] = 'actt';
$_tmp['child'] = ['acww.zip', 'acww21.zip', 'tta.zip'];
$nodes[] = $_tmp;$_tmp2['title'] = '标题2';
$_tmp2['titlekey'] = 'kfij';
$_tmp2['child'] = ['KL7SHR47.zip', 'fdgfdg.zip', 'qweqw.zip'];
$nodes[] = $_tmp2;// 构建调用 Python 脚本的命令
$command = "python3 main.py --folders '" . json_encode($nodes)."'";echo $command; // 输出命令用于调试// 执行命令
exec($command, $output, $return_var);// 检查命令是否成功执行
if ($return_var === 0) {echo "Files processed successfully.";
} else {echo "Error processing files. Output: " . implode("\n", $output);
}
?>
其中$command = "python3 main.py --folders '" . json_encode($nodes)."'"代码中,一定要用单引号把json_encode($nodes)生成的字符串包含起来。
python再去解析字符串
import os
import json
import argparseif __name__ == "__main__":parser = argparse.ArgumentParser(description="Process file operations.")parser.add_argument("--folders", required=True, help="Folders information in JSON format.")args = parser.parse_args()try:folders = json.loads(args.folders) # 尝试解析JSONexcept json.JSONDecodeError as e:print(f"JSON解析错误: {e}")