好的,所以这段代码是一个烧瓶服务器内的http响应。我不认为这些信息会有任何用处,但如果您需要了解它,那就是它。
本代码假设从post请求中读取名称并写入文件。 然后它检查一个名为saved.txt的文件,该文件存储在FILES字典中。 如果我们在saved.txt文件中找不到我们的文件名,我们会将文件名附加到保存的文件中。
APIResponce函数只是一个json转储
目前它似乎根本没有附加。该文件写得很好,但附加没有通过。
另外顺便说一下,这是在Linino上运行的,它只是Linux的发行版。
def post(self):
try:
## Create the filepath so we can use this for mutliple schedules
filename = request.form["name"] + ".txt"
path = "/mnt/sda1/arduino/www/"
filename_path = path + filename
#Get the data from the request
schedule = request.form["schedule"]
replacement_value = schedule
#write the schedule to the file
writefile(filename_path,replacement_value)
#append the file base name to the saved file
append = True
schedule_names = readfile(FILES['saved']).split(" ")
for item in schedule_names:
if item == filename:
append = False
if append:
append_to = FILES['saved']
filename_with_space =filename + " "
append(append_to,filename_with_space)
return APIResponse({
'success': "Successfully modified the mode."
})
except:
return APIResponse({
'error': "Failed to modify the mode"
})
以下是请求的功能
def writefile(filename, data):
#Opens a file.
sdwrite = open(filename, 'w')
#Writes to the file.
sdwrite.write(data)
#Close the file.
sdwrite.close()
return
def readfile(filename):
#Opens a file.
sdread = open(filename, 'r')
#Reads the file's contents.
blah = sdread.readline()
#Close the file.
sdread.close()
return blah
def append(filename,data):
## use mode a for appending
sdwrite = open(filename, 'a')
## append the data to the file
sdwrite.write(data)
sdwrite.close()
答案 0 :(得分:0)
可能是 bool 对象追加且功能名称追加是否相同?当我尝试它时,Python抱怨" TypeError:' bool'对象不可调用"