我该如何处理:log.write len([name

时间:2017-06-01 11:15:11

标签: python python-2.7

我遇到了问题。我不知道如何“连接”log.write和len 它的第10行

import os
import os.path
import time


DIR = '/home/richard/DB/'

#while a != 0:
log = open("logfile.log","wt")
log.write len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
log.close()

3 个答案:

答案 0 :(得分:0)

使用:

log.write(str(len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])))

答案 1 :(得分:0)

小语法错误

import os
import os.path
import time


DIR = '/home/richard/DB/'

#while a != 0:
log = open("logfile.log","wt")
log.write(len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]))
log.close()

答案 2 :(得分:0)

您需要将字符串传递给log.write

log.write("{}".format(len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]))

log.write(str(len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]))