简体   繁体   English

tempfile.TemporaryFile似乎为空,即使它不是

[英]tempfile.TemporaryFile seems empty even though it's not

I am writing to a temporary file by downloading the file from S3. 我正在通过从S3下载文件来写入临时文件。 When I open the downloaded file (called 3 ) in my text editor, I can see all the lines of text. 在文本编辑器中打开下载的文件(称为3 )时,可以看到所有文本行。 But my code returns nothing when I try to read the file line by line. 但是,当我尝试逐行读取文件时,我的代码未返回任何内容。

After running the code, the temporary file is created in the directory of the Python script and doesn't disappear. 运行代码后,临时文件将在Python脚本的目录中创建,并且不会消失。

import tempfile
import os

import boto3

s3 = boto3.client('s3')

with tempfile.TemporaryFile() as tf:
  try:
    s3.download_file(
      Bucket='the-chumiest-bucket',
      Key='path/to/the/file.txt',
      Filename=str(tf.name)
    )
  except Exception as e:
    print('error:', e)

  tf.flush()
  tf.seek(0, os.SEEK_END)

  for line in tf.readlines():
    print('line:', line)

If I run 如果我跑步

with open('3', 'r') as f:
  for line in f.readlines():
    print(line)

I get the lines, so this could be a workaround, but I've seen many people read lines from a tempfile using this exact method. 我知道了这些行,因此这可能是一种解决方法,但是我已经看到许多人使用此精确方法从tempfile中读取行。

Expected Result: 预期结果:

I get the lines within file.txt printed. 我得到file.txt的行打印。

Actual Result: 实际结果:

I get nothing printed. 我什么也没打印。

Edit #1 编辑#1

Changed tf.seek(0, os.SEEK_END) to tf.seek(0, os.SEEK_SET) (thanks @Barmar) and still no lines being printed. tf.seek(0, os.SEEK_END)更改为tf.seek(0, os.SEEK_SET) (感谢@Barmar),仍然没有打印行。 Just one blank line. 仅一个空白行。

You're seeking to the end of the file. 您正在寻找文件结尾。 There's nothing more to read when you're at the end. 当您结束时,没有更多的阅读内容了。 You should see to the beginning. 您应该从头开始。

tf.seek(0, os.SEEK_SET)

I suspect the other problem is that you're updating the file outside of the tf stream. 我怀疑另一个问题是您正在tf流之外更新文件。 It's not going back to the filesystem to read the file contents. 它不会返回文件系统来读取文件内容。 tf.flush() flushes the output buffer, but that doesn't do anything since you haven't written to the stream. tf.flush()刷新输出缓冲区,但这不会做任何事情,因为您尚未写入流。

Instead of seeking in the tf stream, reopen the file: 重新打开文件,而不是在tf流中进行搜索:

with open(tf.name) as tf1:
  for line in tf1.readlines():
    print('line:', line)

Note that you should be using tempfile.NamedTemporaryFile to get a file that's named. 请注意,您应该使用tempfile.NamedTemporaryFile来获取已命名的文件。 And reopening the file only works on Unix, not Windows. 重新打开该文件仅适用于Unix,不适用于Windows。 You might want to use tempfile.mkstemp() instead, as I don't think it has that OS-dependency. 您可能要改用tempfile.mkstemp() ,因为我不认为它具有操作系统依赖性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python的tempfile.TemporaryFile()中指定文本模式? - How to specify text mode in Python's tempfile.TemporaryFile()? Django tempfile.TemporaryFile 出现“无法确定文件大小”错误 - Django "Unable to determine the file's size" error with tempfile.TemporaryFile Python - tempfile.TemporaryFile无法读取; 为什么? - Python - tempfile.TemporaryFile cannot be read; why? tempfile.TemporaryFile与StringIO - tempfile.TemporaryFile vs. StringIO NotImplementedError:只能使用tempfile.TemporaryFile - NotImplementedError: Only tempfile.TemporaryFile is available for use Python> 3中的tempfile.TemporaryFile和tempfile.NamedTemporaryFile是否相同 - Are tempfile.TemporaryFile and tempfile.NamedTemporaryFile same in Python > 3 将tempfile.TemporaryFile分配给变量并执行时出错 - Error when assigning tempfile.TemporaryFile to a variable and executing 如何在Google App Engine上的django中避免NotImplementedError“只有tempfile.TemporaryFile可以使用”? - How to avoid NotImplementedError “Only tempfile.TemporaryFile is available for use” in django on Google App Engine? tempfile.TemporaryFile() 在分配给变量后不适用于 sqlite3 - tempfile.TemporaryFile() does not work with sqlite3 after being assigned to a variable Python 认为我的文件是空的,尽管它不是 - Python thinks my file is empty even though it's not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM