2017-06-01 58 views
1

我有一个问题,我不知道如何“连接” log.write和len 其对10日线我该如何处理是:log.write LEN([名

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

'log.write(LEN(...))' – Jkdc

+0

回溯(最近通话最后一个): 文件 “/home/richard/db1.py”,13号线, 如果os.path.isfile(os.path.join(DIR,name))]))log.write(len([name for os.listdir(DIR)中的名称)) TypeError:预期字符串或其他字符缓冲对象 – Richard

回答

0

使用:

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

回溯(最近一次调用最后一次): 文件“/home/richard/db1.py”,第13行,在 log.write(len([name for name in os.li stdir(DIR)if os.path.isfile(os.path.join(DIR,name))])) TypeError:期望一个字符串或其他字符缓冲对象 – Richard

+0

使用'str()',更新我的答案,请检查 –

+0

感谢您的帮助! – Richard

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() 
+0

回溯(最近一次调用最后一次): 文件“/home/richard/db1.py”,第13行,在 log.write(len([如果os.path中的名称在os.listdir(DIR)中)。 isfile(os.path.join(DIR,name))])) TypeError:预期一个字符串或其他字符缓冲区对象 – Richard

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))]))