我会再做连接txt文件之间的空间,几乎一切顺利,但 的出文件具有类似l o r e m i p s u m
串联文本文件创建文件,每个字母
每个字母 之间的空间,这里是我的代码
import glob
all = open("all.txt","a");
for f in glob.glob("*.txt"):
print f
t = open(f, "r")
all.write(t.read())
t.close()
all.close()
我工作在Windows 7上,蟒蛇2.7
编辑
也许有更好的方法来连接文件?
EDIT2
我现在解码问题:
Traceback (most recent call last):
File "P:\bwiki\BWiki\MobileNotes\export\999.py", line 9, in <module>
all.write(t.read())
File "C:\Python27\lib\codecs.py", line 671, in read
return self.reader.read(size)
File "C:\Python27\lib\codecs.py", line 477, in read
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 18: invalid
continuation byte
import codecs
import glob
all =codecs.open("all.txt", "a", encoding="utf-8")
for f in glob.glob("*.txt"):
print f
t = codecs.open(f, "r", encoding="utf-8")
all.write(t.read())
使用简单批处理命令连接文本文件的最佳方法。您可以简单地将文件一起添加,就好像它们是数字一样。 – PlamZ 2015-01-15 16:48:46
我怀疑这个错误可能与你打开'all.txt'两次有关。一旦将其分配给“全部”,并在另一次将其分配给循环时。 'all.txt'将匹配glob'“* .txt”'。 – 2015-01-15 16:51:48
@AlexBliskovsky我不认为会产生所描述的症状,但你是对的,这是一个错误*也*。 – zwol 2015-01-15 16:52:42