编辑:让我以我是Python的新手作为开头。
我正在尝试列出目录结构并将输出打印到文件中。 SHELL会显示所需的结果,但是写入的文件仅显示“ None”
import os
import datetime
now = datetime.datetime.now()
def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print('{}{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print('{}{}'.format(subindent, f))
with open('c:/listing.txt',"w") as listing:
listing.write(str(print(list_files('c:/test/'))))
Shell显示以下内容,这是正确的。但是,写入的文件只有一行,说“无”
>>>
RESTART: C:/Users/ABC/AppData/Local/Programs/Python/Python37-32/test.py
/
eula.2052.txt
eula.3082.txt
globdata.ini
install.exe
install.ini
install.res.1028.dll
install.res.1031.dll
install.res.1033.dll
install.res.1036.dll
install.res.1040.dll
install.res.1041.dll
None
>>>