Вопрос

Ok so this snippet of code is a http response inside of a flask server. I dont think this information will be of any use but its there if you need to know it.

This Code is suppose to read in the name from the post request and write to a file. Then it checks a file called saved.txt which is stored in the FILES dictionary. If we do not find our filename in the saved.txt file we append the filename to the saved file.

APIResponce function is just a json dump

At the moment it doesn't seem to be appending at all. The file is written just fine but append doesn't go thru.

Also btw this is being run on Linino, which is just a distribution of 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"
        })

Here are the requested functions

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()
Это было полезно?

Решение

Could it be that the bool object append and the function name append are the same? When I tried it, Python complained with "TypeError: 'bool' object is not callable"

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top