title: Can I pickle Python objects in memory instead of a physical file?
body: <p>I have seen and written code to pickle objects in Python. But they all create physical file to contain data. I want that I write the data in memory and read it and pickle it and transfer it.</p>
  
  <p>Is it even possible ?</p>
  
  <pre><code>from PIL import ImageGrab
  import io
  import codecs
  import pickle
  
  
  # Model class to send Process Data to the server
  class ProcessData:
   process_id = 0
   project_id = 0
   task_id = 0
   start_time = 0
   end_time = 0
   user_id = 0
   weekend_id = 0
  
  # Model class to send image data to the server
  class ProcessScreen:
   process_id = 0
   image_data = bytearray()
  
  
  image_name = "Dharmindar_screen.jpg"
  ImageGrab.grab().save(image_name,"JPEG")
  
  image_data = None
  with codecs.open(image_name,'rb') as image_file:
   image_data = image_file.read()
  
  
  
  serialized_process_data = io.BytesIO()
  
  process_data = ProcessData()
  process_data.process_id = 1
  process_data.project_id = 2
  process_data.task_id = 3
  process_data.user_id = 4
  process_data.weekend_id = 5
  process_data.start_time = 676876
  process_data.end_time = 787987
  
  process_screen = ProcessScreen()
  process_screen.process_id = process_data.process_id
  process_screen.image_data = image_data
  
  
  prepared_process_data = (process_data, process_screen)
  
  process_data_serializer = pickle.Pickler()
  process_data_serializer(serialized_process_data).dump(prepared_process_data)
  
  print('Data serialized.')
  
  
  if process_data_serializer is not None:
      d = process_data_serializer.getvalue()
      deserialized_data = None
      with open(d, 'rb') as serialized_data_file:
          process_deserializer = pickle.Unpickler(serialized_data_file)
          deserialized_data = process_deserializer.load()
  else:
      print('Empty')
  </code></pre>
  
  <p>The above code throws the TypeError: Required argument 'file' (pos 1) not found</p>