在Windows和Python3下,我正在将一些使用os.system()
的旧代码转换为使用subprocess.run()
,但是在将输出捕获到stdout和stderr时遇到了麻烦。
这是我从简单的dir c:\
中得到的内容:
r = subprocess.run(['dir', 'c:\\'], check=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
r.stdout
Out[56]: '$GetCurrent\t\t Program\\ Files\t Users\t cygwin64\n$Recycle.Bin\t\t Program\\ Files\\ (x86) WPy64-3740 findEm\nBOOTNXT\t\t\t ProgramData\t\t Windows\t microchip\nBrother\t\t\t Program_Files\t\t antiword pagefile.sys\nDocuments\\ and\\ Settings Recovery\t\t bin\t swapfile.sys\nPerfLogs\t\t System\\ Volume\\ Information bootmgr\n'
type(r.stdout)
Out[57]: str
输出中的所有'\ t'(我想是标签)和'\ n'(换行符)是什么?
如何将结果中的字符串'\ t'和'\ n'转换为实际的制表符和换行符以进行打印?