我想使用 python 将多个 TEXTS 合并为一个 TEXT,但它显示 "io.UnsupportedOperation: not writable"。有没有人可以帮忙谢谢
I want to use python to merge multiple TXTS into one TXT, but it shows me "io.UnsupportedOperation: not writable". does anyone can help thanks
我想使用 python 将多个 TEXTS 合并为一个 TEXT,但它显示 "io.UnsupportedOperation: not writable"。有没有人可以帮忙谢谢:
import os
sourceFileDir = 'C:\Users\QWE\Desktop\10000_Tweet_Sample_Eng'
filenames = os.listdir(sourceFileDir)
file = open('C:\Users\QWE\Desktop\result.txt', 'r')
for filename in filenames:
filepath = sourceFileDir + '\' + filename
for line in open(filepath):
file.writelines(line)
file.write('\n')
file.close()
您正在以只读方式打开结果文件。
尝试更改:
file = open('C:\Users\QWE\Desktop\result.txt', 'r')
收件人:
file = open('C:\Users\QWE\Desktop\result.txt', 'w')
更多关于打开功能的使用方法,请查看这里:
https://docs.python.org/3.7/library/functions.html#open
对于您后续的问题,可能是您打开的文件需要特定的编码,请尝试更改:
file = open('C:\Users\QWE\Desktop\result.txt', 'w')
收件人:
file = open('C:\Users\QWE\Desktop\result.txt', 'w', encoding='utf-8')
并且:
for line in open(filepath):
收件人:
for line in open(filepath, encoding='utf-8'):
我想使用 python 将多个 TEXTS 合并为一个 TEXT,但它显示 "io.UnsupportedOperation: not writable"。有没有人可以帮忙谢谢:
import os
sourceFileDir = 'C:\Users\QWE\Desktop\10000_Tweet_Sample_Eng'
filenames = os.listdir(sourceFileDir)
file = open('C:\Users\QWE\Desktop\result.txt', 'r')
for filename in filenames:
filepath = sourceFileDir + '\' + filename
for line in open(filepath):
file.writelines(line)
file.write('\n')
file.close()
您正在以只读方式打开结果文件。
尝试更改:
file = open('C:\Users\QWE\Desktop\result.txt', 'r')
收件人:
file = open('C:\Users\QWE\Desktop\result.txt', 'w')
更多关于打开功能的使用方法,请查看这里:
https://docs.python.org/3.7/library/functions.html#open
对于您后续的问题,可能是您打开的文件需要特定的编码,请尝试更改:
file = open('C:\Users\QWE\Desktop\result.txt', 'w')
收件人:
file = open('C:\Users\QWE\Desktop\result.txt', 'w', encoding='utf-8')
并且:
for line in open(filepath):
收件人:
for line in open(filepath, encoding='utf-8'):