我有一个循环遍历.bat文件目录的Python脚本。对于每个文件,它运行其批处理脚本,然后将输出与包含文件输出的保存文件进行比较,以测试.bat是否正确。现在我正在测试它,我把它作为测试文件:
@echo off
echo Good morning world and all who inhabit it! :D
然后我有“解决方案”文件:
Good morning world and all who inhabit it! :D
这是检查文件的代码:
for _, _, files in os.walk(dir):
for f in files:
result = []
fpath = os.path.join(jobs, os.path.basename(f))
params = [fpath]
result.append(subprocess.check_output([params]))
expectedname = dir2 + os.path.basename(f) + ".test"
expectedfile = open(expectedname, 'r')
expected = []
expected.append(expectedfile.read())
print '\nEXPECTED: %s\nACTUAL: %s' % (expected, result)
if result == expected:
print "\n%s is correct!\n" % (os.path.basename(f))
else:
print "\n%s gave incorrect output!\n" % (os.path.basename(f))
问题是,当.bat文件运行并生成其输出时,它会在末尾添加“\r\n
”。我尝试通过将“\r\n
”放在解决方案文件中来解决它,但在读取时将其更改为“\\r\\n
”。我该如何解决这个问题?
注意:显然,SO的代码格式突出显示“和”,默认情况下,回声中单词的存在根本不会影响它的功能。只是不想让它混淆哈哈
答案 0 :(得分:1)
您可以在批次中尝试此操作:
@echo off
<nul set /p "=Good morning world and all who inhabit it! :D"
顺便说一句。我对python没有任何线索。
我的第二个建议来自CoreUtils for Windows的tr
:
@ECHO OFF &SETLOCAL
ECHO Good morning world and all who inhabit it! :D | tr -d \r