Python 中 truncate() 方法的行为 [英] Behaviour of truncate() method in Python

查看:17
本文介绍了Python 中 truncate() 方法的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自 Zed Shaw 的 Python 教程的练习 16.我很难理解 truncate 函数在这种情况下究竟做了什么.所以逻辑是我们打开一个文件然后...缩短它?为了什么?这里到底发生了什么?

This is from exercise 16 from Zed Shaw's Python tutorials. I'm having a hard time understanding what exactly the truncate function does in this case. So the logic is that we open a file and then...shorten it? For what? What exactly is happening here?

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file.  Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

target.write(line1 + "
" + line2 + "
" + line3 + "
")

print "And finally, we close it."
target.close()

推荐答案

你的怀疑是对的.

首先,file.truncate 这样做:

First, file.truncate does this:

截断文件的大小.如果存在可选的 size 参数,则文件将被截断为(最多)该大小.大小默认为当前位置...

Truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position…

与 Zed 的描述不太一样——如果当前位置是文件的开头,它只会清空文件"——但因为我们刚刚打开文件(而不是在 a 模式下),当前位置是开始,所以不相关.我们正在截断为一个空文件.

Not quite the same as Zed's description—it only "empties the file" if the current position is the start of the file—but since we just opened the file (and not in a mode), the current position is the start, so that isn't relevant. We're truncating to an empty file.

这一切都很好,除了open已经这样做了:

Which is all well and good, except that open already does that:

mode 最常用的值是 'r' 用于读取,'w' 用于写入(如果文件已经存在,则截断文件)......

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists) …

因此,我们打开文件,如果它不存在则创建它,如果存在则将其截断为 0 字节.然后,在下一行,我们将其截断为 0 个字节.

So, we open the file, creating it if it doesn't exist and truncating it to 0 bytes if it does. Then, on the next line, we truncate it to 0 bytes.

(截断文件.再见!"消息非常具有误导性,因为我们已经截断了它.假设您在该行上放置了一个断点并决定在执行之前终止程序......)

(That "Truncating the file. Goodbye!" message is pretty misleading, since we've already truncated it. Imagine you put a breakpoint on that line and decided to kill the program before executing it…)

但是请注意,这不是 Zed 犯的一些愚蠢的错误;他这样做似乎是为了在学习练习 #5 中强调这一点:

But notice that this isn't some silly mistake by Zed; he appears to have done this specifically to make the point in study drill #5:

如果你用'w'模式打开文件,那么你真的需要target.truncate()吗?阅读 Python 的 open 函数的文档,看看是否属实.

If you open the file with 'w' mode, then do you really need the target.truncate()? Read the documentation for Python's open function and see if that's true.

这篇关于Python 中 truncate() 方法的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆