作者proud (hc)
看板Python
标题[问题] os.listdir後open text问题
时间Mon Apr 25 14:37:44 2016
python 新手想请问openfile的问题
一段code如下
# -*- coding: UTF-8 -*-
import os, sys
path= "/home/puoud/documents"
dirs = os.listdir(path)
for file in dirs:
print file
若我想将documents内的每一个.txt都read
请问在下一个段落open(file,'r').open()
出错在哪里?
出错
IOError: [Errno 2] No such file or directory: xxxx.txt
请问怎麽打比较正确?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.240.33.180
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1461566266.A.5EF.html
1F:→ yeh6: open的路径是path+file吧04/25 14:55
感谢!
2F:推 tiefblau: 最简单的解法 open(path+'/'+file,'r').04/25 17:20
解决了,感谢!
※ 编辑: proud (180.204.20.230), 04/25/2016 22:07:37
3F:推 flarehunter: 可以用os.path.join合并 04/25 22:38
4F:→ amigcamel: 没人提glob吗? 04/30 12:22