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

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

问题描述

这是从Zed Shaw的Python教程练习16 。在这种情况下,我很难理解 truncate 函数的作用。所以逻辑是我们打开一个文件然后...缩短它?为了什么?这里发生了什么事情?

pre $ from sys import argv

script,filename = argv

print我们要抹去%r。 %filename
print如果你不想这样做,可以按CTRL-C(^ C)。
print如果你确实需要这个,点击返回。

raw_input(?)

打印打开文件...
target =打开(文件名,'w')

打印截断文件,再见!
target.truncate()

print现在我要问你三行。

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

打印我将把这些文件写入文件。

target.write(line1 +\ n+ line2 +\ n+ line3 +\\\


print最后,我们关闭它。
target.close()


解决方案

如果您有任何疑问,请点击这里。

首先, file.truncate 这样做:
$ b


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


与Zed的描述不太一样,只是清空文件是文件的开始 - 但是由于我们只是打开文件(而不是在 a 模式),所以当前位置是开始,所以这是不相关的。我们截断了一个空的文件。



除了 open 已经做到了:


模式最常用的值是'r'阅读,'w'截断文件,如果它已经存在)...

所以,我们打开文件,创建它,如果它不存在和截断如果它是0字节。然后,在下一行,我们把它截断为0字节。

(即截断文件。再见!的消息是非常误导,因为我们已经截断想象一下,你在这条线上放了一个断点,并决定在执行之前杀掉程序...)

但是请注意,这不是Zed的一个愚蠢的错误;他似乎已经做了具体的工作,以使钻研#5的重点:$ b​​


如果您使用'w'模式,那么你真的需要 target.truncate()?阅读Python的打开函数的文档,看看是否为真。



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 + "\n" + line2 + "\n" + line3 + "\n")

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

解决方案

You're right to be suspicious.

First, file.truncate does this:

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…

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.

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

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

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…)

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:

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天全站免登陆