file io - Python IO Gurus: what are the differences between these two methods? -


I have two ways to write binary files: First work with the data received by the server corresponding to the file upload (i.e., a form whose enctype = "multipurpose / form-data"), and as an email attachment Dealt with the other data that is sent (ie, file data received by using the get_payload) parsing the main part of an email message message.

The strange thing is, they are not interchangeable: if I use the first one to save data parsed from email attachment, then it fails; Similarly, the second function fails to handle uploaded file data.

What are the important differences?

This is the first method:

  def write_binary_file (folder, file name, f, chunk_size = 4096): "" "" result = false if confirm_folder (folder) " Write f file data for folder and file name combination: Try: file_obj = open (os.path.join (folder, file_base_name (name of file)), 'west bengal', chunk_size) read_buffer file_chunk (ch, chunk_size ): File_obj.write (file_chunk) file_obj.close () result = except (IOError) is true: print "file_utils.write_binary_file: could not write '% S' in '% s'% (file_base_name (filename ), Folder) Return Results  

This is the second method:

  def write_binary_file (folder, file name is filed): "" "Write binary file data in folder and filename combination" "result = false If confirm_fol der (folder): try: file_obj = open (os.path.join (folder, file_base_name (file name)), 'west Bengal' file_obj.write (filedata) file_obj.close () results = true Excluding (IOError): print "file_utils.write_binary_file: can not write '% s' to '% s'"% (file_base_name (file name), folder) return result  

TSI can understand uploading HTM upload, because this file copy is still allowed to upload I will - I Personally, it does not think that it does not matter, but for uploads of several megabytes, since your HTTP response will be stopped until all uploads are done in any way, a simple CGI script In.

The second method is found in the form of a "file_data" parameter: Everyone has to do this in a new file. (Someone has to read data from the object like the file and still creates an arbitrator object for it)

You can use the second method to save the HTTP data, Therefore, instead of calling the second function with the "F" algorithm provided by your CGI field value, call "f.read ()" - by reading all the data Cause will be Related kind of data to be viewed by the "f" file Method

This means :.

  #second case: write_binary_file (folder, file name, f.read ())  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -