0

I am trying to replicate this bit of code in Python which takes a text stream encoded in base64 and writes it byte by byte to a csv file:

using (FileStream localFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
{
   using (Stream remoteStream = client.DownloadFile(jobId))
   {
     while (!endOfStream)
     {
         bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
         if (bytesRead > 0)
         {
              localFileStream.Write(buffer, 0, bytesRead);
              totalBytes += bytesRead;
         }
         else
         {
              endOfStream = true;
         }
      }
   }
}

Unfortunately I do not know what the equivalent of FileStreamis in Python, so I am unable to translate the code.

walker_4
  • 419
  • 1
  • 7
  • 19
  • 2
    You've already asked this question once; modify your existing question instead of asking another. – David Yaw Jan 24 '17 at 23:50
  • Possible duplicate of [How to write streamed text download to a csv?](http://stackoverflow.com/questions/41839788/how-to-write-streamed-text-download-to-a-csv) – David Yaw Jan 24 '17 at 23:50
  • 1
    I think I will delete the first question, this one is more specific, apologies for the confusion – walker_4 Jan 24 '17 at 23:50
  • Your question seems to boil down to "how do I write files in Python". http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python – David Yaw Jan 24 '17 at 23:55
  • Also, this question has something very similar to what you want to do: Read from a network stream, write to a file. http://stackoverflow.com/questions/39683734/viewing-h264-stream-over-tcp (The code example in the question, not the answer.) – David Yaw Jan 24 '17 at 23:56
  • I understand how to write files in python but am unaware of how to deal with coded downloaded text streams. I will check out the second link you sent. – walker_4 Jan 25 '17 at 00:00
  • Are you looking for Pythons' pickling by chance? https://docs.python.org/2/library/pickle.html You can use it to read/write byte-stream / array similarly to the C# – Milan Jan 25 '17 at 00:09
  • Thank you both. I will check out pickling Milan and see if that is what I need. – walker_4 Jan 25 '17 at 00:15

1 Answers1

0

The equivalent to C#'s FileStream is Python's file object. They both handle reading & writing files, and do not do any major manipulation of the data read/written. (I'm not sure what a "coded download text stream" is, but neither language's file writer is going to decode it on its own.)

(When opened in text mode, Python's file object will normalize line endings, but that's it.)

David Yaw
  • 25,725
  • 4
  • 59
  • 90