问题描述
以下Windows上的Python 3.8上的代码可根据需要运行,但它将数字输出到STDOUT,我不知道为什么。它会递归读取目录并正确输出到文本文件。
import os
targpath = "C:\\users\\ReedActed\\desktop\\" # To direct location of target file
with open(targpath + 'P-drive-test.txt','w',encoding='utf-8') as outfile:
for r,d,f in os.walk('P:\\backup\\'):
for file in f:
fileneat = os.path.join(r,file) + "\n" # Needs the line break
outfile.write(fileneat)
文件看起来正确,但是在STDOUT中我得到了:
99
65
75
47
41
33
什么在创建STDOUT输出?好奇心不是问题。
解决方法
这个答案确实归功于juanpa arrivillaga,但看起来outfile.write()的结果将返回写入的字节数,因为它位于REPL中。感谢您的解决方案!