在烧瓶服务器页面中写入 file.txt
write in a file.txt in the flask server page
这是我在正常 python 脚本中执行的代码,它可以正常工作,但是当涉及到烧瓶函数时,他创建了 file.txt,但没有写入
app = Flask(__name__)
@app.route('/execute',methods=['POST'])
def execute():
message = request.get_json(force=True)
name=message['name']
path="data/"
testname="test.txt"
encodedname="encoded.txt"
output = open(path+testname, "wb")
output.write(name.encode('utf-8'))
这里是第一次写作品
with open(path+testname, 'rb') as infile:
data = infile.readlines()
all_data=""
for oneline in data:
oneline=oneline.decode('utf-8')
print(
oneline)
new_data = re.sub(r'[^\u0600-\u065F\u0670-\u06ef\u0750-\u077f\ufb50-\ufbc1\ufbd3-\ufd3f\ufd50-\ufd8f\ufd50-\ufd8f\ufe70-\ufefc\uFDF0-\uFDFD]+',' ', oneline)
new_data = re.sub('[\ufd3e\ufd3f]',' ',new_data)
new_data = remove_punctuations(new_data)
new_data = new_data+'@\n'
all_data = all_data+new_data
cleanedtest = open(path+"cleanToTest.txt",'wb')
cleanedtest.write(all_data.encode('utf-8'))
但这里没有 (cleanedTest)
您应该使用上下文管理器。您的问题是您需要在写入后关闭该文件。上下文管理器会自动为您完成。
改变这个:
output = open(path+testname, "wb")
output.write(name.encode('utf-8'))
为此:
with open(path+testname, 'wb') as output:
output.write(name.encode('utf-8'))
也改变这个:
cleanedtest = open(path+"cleanToTest.txt",'wb')
cleanedtest.write(all_data.encode('utf-8'))
为此:
with open(path+"cleanToTest.txt", 'wb') as output:
output.write(name.encode(all_data.encode('utf-8'))
这是我在正常 python 脚本中执行的代码,它可以正常工作,但是当涉及到烧瓶函数时,他创建了 file.txt,但没有写入
app = Flask(__name__)
@app.route('/execute',methods=['POST'])
def execute():
message = request.get_json(force=True)
name=message['name']
path="data/"
testname="test.txt"
encodedname="encoded.txt"
output = open(path+testname, "wb")
output.write(name.encode('utf-8'))
这里是第一次写作品
with open(path+testname, 'rb') as infile:
data = infile.readlines()
all_data=""
for oneline in data:
oneline=oneline.decode('utf-8')
print(
oneline)
new_data = re.sub(r'[^\u0600-\u065F\u0670-\u06ef\u0750-\u077f\ufb50-\ufbc1\ufbd3-\ufd3f\ufd50-\ufd8f\ufd50-\ufd8f\ufe70-\ufefc\uFDF0-\uFDFD]+',' ', oneline)
new_data = re.sub('[\ufd3e\ufd3f]',' ',new_data)
new_data = remove_punctuations(new_data)
new_data = new_data+'@\n'
all_data = all_data+new_data
cleanedtest = open(path+"cleanToTest.txt",'wb')
cleanedtest.write(all_data.encode('utf-8'))
但这里没有 (cleanedTest)
您应该使用上下文管理器。您的问题是您需要在写入后关闭该文件。上下文管理器会自动为您完成。
改变这个:
output = open(path+testname, "wb")
output.write(name.encode('utf-8'))
为此:
with open(path+testname, 'wb') as output:
output.write(name.encode('utf-8'))
也改变这个:
cleanedtest = open(path+"cleanToTest.txt",'wb')
cleanedtest.write(all_data.encode('utf-8'))
为此:
with open(path+"cleanToTest.txt", 'wb') as output:
output.write(name.encode(all_data.encode('utf-8'))