Windows 平台上 Anaconda 中 Python 的 Perl 替换
Perl substitution from Python in Anaconda on Windows platform
在 Windows 上的 Anaconda shell 环境中安装了 perl、m2-base 和可能安装的其他一些软件包:
$ echo "/" > junk
$ more junk
"/"
$ perl -pi.bak -e "s/\"\/\"/\"\\\"/" junk
$ more junk junk.bak
::::::::::::::
junk
::::::::::::::
"\"
::::::::::::::
junk.bak
::::::::::::::
"/"
我想在 Python 中复制它。我的脚本是这样的:
import subprocess
cmd = 'perl -pi.bak -e "s/\"\/\"/\"\\\"/" junk'
subprocess.call(cmd, shell = True)
给出以下输出:
$python test_perl.py
Substitution replacement not terminated at -e line 1.
我尝试了不同的反斜杠组合、不同的引号样式以及在 perl 中使用不同的定界符(即用 @
之类的东西替换 /
),但似乎无法理解了解如何破解这个坚果。
更新
subprocess.call(['perl', '-pi.bak', '-e', "s!\\"\/\"!\"\\\\"!", 'junk'], shell = True)
有效,但我很困惑为什么 subprocess
不需要额外的引号来封装 perl switch 语句。任何见解将不胜感激。
--
有关我实际工作的更多信息,我正在尝试在 Anaconda 环境中的 Windows 平台上安装专为 Linux/Unix 设计的 Python 模块。一方面,我需要在模块的某些文件中将“/”替换为“\”。我知道我可以直接编辑文件并使用 os.path.split
而不是 split("/")
之类的东西,但我正在尝试创建一个完成所有工作的文件,所以需要做的就是克隆git 存储库和 运行 安装脚本。
以下 python 演示代码模拟 perl -i.bak ...
行为。
问题描述没有解释为什么 OP 求助于 Perl 帮助进行简单替换并保留 .bak
文件作为备份副本。
Python有足够的肌肉来执行这样的操作,只需几行代码。
import os
ext_bak = '.bak'
file_in = 'path_substitute.txt'
file_bak = file_in + ext_bak
# remove backup file if exists
if os.path.exists(file_bak):
os.remove(file_bak)
# rename original file to backup
os.rename(file_in,file_bak);
f = open(file_bak,'r') # read from backup file
o = open(file_in, 'w') # write to a file with original name
for line in f:
o.write(line.replace('/','\')) # replace / with \ and write
# close files
f.close()
o.close()
输入path_substitute.txt
some path /opt/pkg/dir_1/file_1 word_1
other path /opt/pkg/dir_2/file_2 word_2
one more /opt/pkg/dir_3/file_3 word_3
输出path_substitute.txt
some path \opt\pkg\dir_1\file_1 word_1
other path \opt\pkg\dir_2\file_2 word_2
one more \opt\pkg\dir_3\file_3 word_3
在 Windows 上的 Anaconda shell 环境中安装了 perl、m2-base 和可能安装的其他一些软件包:
$ echo "/" > junk
$ more junk
"/"
$ perl -pi.bak -e "s/\"\/\"/\"\\\"/" junk
$ more junk junk.bak
::::::::::::::
junk
::::::::::::::
"\"
::::::::::::::
junk.bak
::::::::::::::
"/"
我想在 Python 中复制它。我的脚本是这样的:
import subprocess
cmd = 'perl -pi.bak -e "s/\"\/\"/\"\\\"/" junk'
subprocess.call(cmd, shell = True)
给出以下输出:
$python test_perl.py
Substitution replacement not terminated at -e line 1.
我尝试了不同的反斜杠组合、不同的引号样式以及在 perl 中使用不同的定界符(即用 @
之类的东西替换 /
),但似乎无法理解了解如何破解这个坚果。
更新
subprocess.call(['perl', '-pi.bak', '-e', "s!\\"\/\"!\"\\\\"!", 'junk'], shell = True)
有效,但我很困惑为什么 subprocess
不需要额外的引号来封装 perl switch 语句。任何见解将不胜感激。
--
有关我实际工作的更多信息,我正在尝试在 Anaconda 环境中的 Windows 平台上安装专为 Linux/Unix 设计的 Python 模块。一方面,我需要在模块的某些文件中将“/”替换为“\”。我知道我可以直接编辑文件并使用 os.path.split
而不是 split("/")
之类的东西,但我正在尝试创建一个完成所有工作的文件,所以需要做的就是克隆git 存储库和 运行 安装脚本。
以下 python 演示代码模拟 perl -i.bak ...
行为。
问题描述没有解释为什么 OP 求助于 Perl 帮助进行简单替换并保留 .bak
文件作为备份副本。
Python有足够的肌肉来执行这样的操作,只需几行代码。
import os
ext_bak = '.bak'
file_in = 'path_substitute.txt'
file_bak = file_in + ext_bak
# remove backup file if exists
if os.path.exists(file_bak):
os.remove(file_bak)
# rename original file to backup
os.rename(file_in,file_bak);
f = open(file_bak,'r') # read from backup file
o = open(file_in, 'w') # write to a file with original name
for line in f:
o.write(line.replace('/','\')) # replace / with \ and write
# close files
f.close()
o.close()
输入path_substitute.txt
some path /opt/pkg/dir_1/file_1 word_1
other path /opt/pkg/dir_2/file_2 word_2
one more /opt/pkg/dir_3/file_3 word_3
输出path_substitute.txt
some path \opt\pkg\dir_1\file_1 word_1
other path \opt\pkg\dir_2\file_2 word_2
one more \opt\pkg\dir_3\file_3 word_3