高洛峰2017-04-18 09:05:04
If you want to insert content in front of an existing file, then performance is not a big concern.
Because you have to read and write the contents of this file again no matter what, this is determined by the logic of the file system.
黄舟2017-04-18 09:05:04
file.seek(0) This is the offset set to 0, which is the very beginning.
You can use file.tell() to confirm.
You can refer to the two web pages.
https://docs.python.org/2/tutorial/inputoutput.html
http://www.tutorialspoint.com/python/file_seek.htm
阿神2017-04-18 09:05:04
No need to seek(0)
r+
The file pointer of model is at the beginning of the file.
Also, you must traverse the file content
PHPz2017-04-18 09:05:04
Let’s discuss this issue in two points:
How to insert content into the head or middle of the file efficiently. Efficiency refers to less time (faster speed) and resource utilization.
How to write enough pythonic code to handle this
Let me tell you my conclusion first (if you have any other opinions, please feel free to discuss, maybe I am wrong):
Can’t do it
As long as it’s not too poorly written or too hard to read, I think a mediocre or longer code is fine (or even better)
Quoting the words of the great god to prove it:
Python makes a lot of things easy and contains libraries and wrappers for a lot of common operations, but the goal is not to hide fundamental truths.
The fundamental truth you are encountering here is that you generally can't prepend data to an existing flat structure without rewriting the entire structure. This is true regardless of language.
There are ways to save a filehandle or make your code less readable, many of which are provided in other answers, but none change the fundamental operation: You must read in the existing file, then write out the data you want to prepend, followed by the existing data you read in.
By all means save yourself the filehandle, but don't go looking to pack this operation into as few lines of code as possible. In fact, never go looking for the fewest lines of code -- that's obfuscation, not programming.
By Nicholas Knight
Reference:
Prepend a line to an existing file in Python
Questions I answered: Python-QA
PHP中文网2017-04-18 09:05:04
There is no way to insert content directly at the beginning, you must read the file once.
I have thought about this problem before. At that time, I was decompiling a python project and needed to batch process all files. Insert the following content at the beginning of each python file:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
I also tried many methods. I also looked for methods online at that time, but to no avail.
So, in the end, I used the simplest and most violent method, using osd.walk
to traverse all python files, write the contents at the beginning into a new file, then read the contents of the original file, write it into the new file, and then rename the new file. is the original file name.
Although it is very violent, I found out later that it is actually very fast, maybe because the file is not too big. ^_^