nodejs调用python单反斜杠“\”转正斜杠“/“

214 阅读1分钟

最近在node中传入路径(eg: c:\usr\file)调用python,但python不识别反斜杠(\),因为会当作转义进行处理,而node中在window下识别(\),并且_dirname中也是(\),为了让python运行,所以进行替换。

  let input="c:\user\myfile\file"
  let forward=input.replace(/\\/g,'%5C')//"c:%5Cuser%5Cmyfile%5Cfile"
  input=forward.replace(/%5C/g,'/')//"c:/user/myfile/file"