Sinisterly
Whats the point of the truncate() function for writing to files? - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: Whats the point of the truncate() function for writing to files? (/Thread-Whats-the-point-of-the-truncate-function-for-writing-to-files)



Whats the point of the truncate() function for writing to files? - Alan Turing - 01-07-2014

What's the point of the truncate() function?

For example in this PoC:


PHP Code:
from sys import argv script, filename = argv print "Okay, we are going to write to the file %r" % filename print "You can print what you like, hit enter to continue, or press Ctrl C to exit(^C)" raw_input("?") print "Opening the selected file now..." HPP = open(filename, 'w') print "Truncating the file" HPP.truncate() [color=#FF1493]#The fuck is truncate() for?[/color] print "Okay, print what you want into the .txt file" line1 = raw_input("Line 1: ") line2 = raw_input("Line 2: ") line3 = raw_input("Line 3: ") line4 = raw_input("Line 4: ") line5 = raw_input("Line 5: ") line6 = raw_input("Line 6: ") print "Okay, whatever you input above^ will be written to the filename" HPP.write(line1) HPP.write("\n") HPP.write(line2) HPP.write("\n") HPP.write(line3) HPP.write("\n") HPP.write(line4) HPP.write("\n") HPP.write(line5) HPP.write("\n") HPP.write(line6) HPP.write("\n") print "Thank you, and goodbye"

I can't find anything on the truncate funct. itself. Is it really necessary to use, or is it mandatory to have it? I know I could just delete that line of code, and attempt running it anyway, but I want someones input.


RE: Whats the point of the truncate() function for writing to files? - Dyme - 01-07-2014

Taken from the official python documentation:
Code:
file.truncate([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. The current file position is not changed. Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants.

This being said, I have never used it nor do I see the need for it to be used in your case.


RE: Whats the point of the truncate() function for writing to files? - Hellborn - 01-07-2014

This might help you out.
http://stackoverflow.com/questions/4562100/why-truncate-when-we-open-a-file-in-w-mode-in-python


RE: Whats the point of the truncate() function for writing to files? - Alan Turing - 01-07-2014

(01-07-2014, 12:53 AM)Pens Wrote: Taken from the official python documentation:
Code:
file.truncate([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. The current file position is not changed. Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants.

This being said, I have never used it nor do I see the need for it to be used in your case.

Just as I expected if I removed the truncate line it still works, thanks for the response

(01-07-2014, 12:54 AM)Necrosis Wrote: This might help you out.
http://stackoverflow.com/questions/4562100/why-truncate-when-we-open-a-file-in-w-mode-in-python

Will check it out, thanks for the reply