【问题标题】:What is the Python equivalent to FileStream in C#?什么是 Python 等价于 C# 中的 FileStream?
【发布时间】:2017-01-24 23:48:48
【问题描述】:

我正在尝试在 Python 中复制这段代码,该代码采用 base64 编码的文本流并将其逐字节写入 csv 文件:

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;
         }
      }
   }
}

不幸的是,我不知道 Python 中 FileStream 的等价物是什么,所以我无法翻译代码。

【问题讨论】:

标签: c# python


【解决方案1】:

相当于 C# 的 FileStream 是 Python 的文件对象。它们都处理读取和写入文件,并且不对读取/写入的数据进行任何主要操作。 (我不确定“编码下载文本流”是什么,但两种语言的文件编写器都不会自行解码。)

(在文本模式下打开时,Python 的文件对象会规范化行尾,仅此而已。)

【讨论】:

  • 谢谢大卫,你似乎是对的,不幸的是我似乎问错了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多