相关疑难解决方法(0)

Python:用f.next()迭代时在文件中倒带一行

当你使用f.next()迭代文件时,Python的f.tell不能像我预期的那样工作:

>>> f=open(".bash_profile", "r")
>>> f.tell()
0
>>> f.next()
"alias rm='rm -i'\n"
>>> f.tell()
397
>>> f.next()
"alias cp='cp -i'\n"
>>> f.tell()
397
>>> f.next()
"alias mv='mv -i'\n"
>>> f.tell()
397
Run Code Online (Sandbox Code Playgroud)

看起来它给你缓冲区的位置,而不是你刚接下来的位置().

我以前使用seek/tell 技巧在使用readline()迭代文件时回绕一行.使用next()时有没有办法倒回一行?

python next seek

10
推荐指数
2
解决办法
6975
查看次数

write和tempfile.write之间的区别

请解释以下内容:

def feed(data):
  import os
  print "DATA LEN: %s" % len(data)
  f = open("copy", "w")
  f.write(data)
  f.close()
  print "FILE LEN: %s" % os.stat("copy").st_size
  t = tempfile.NamedTemporaryFile()
  t.write(data)
  print "TEMP LEN: %s" % os.stat(t.name).st_size
  t.close()

feed(x)
DATA LEN: 11004
FILE LEN: 11004
TEMP LEN: 8192
Run Code Online (Sandbox Code Playgroud)

为什么会有所不同,我可以解决温度问题吗?结局似乎被​​砍了。

在2.6、2.7上测试

python file

4
推荐指数
1
解决办法
82
查看次数

标签 统计

python ×2

file ×1

next ×1

seek ×1