I need to add a single line to the first line of a text file and it looks like the only options available to me are more lines of code than I would expect from python. Something like this:
Is there no easier way? Additionally, I see this two-handle example more often than opening a single handle for reading and writing ('r+') - why is that? asked Dec 15, 2010 at 19:59
1 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. answered Dec 15, 2010 at 20:24
Nicholas KnightNicholas Knight 15.5k4 gold badges44 silver badges56 bronze badges 2 I would stick with separate reads and writes, but we certainly can express each more concisely: Python2:
Python3:
Note: file() function is not available in python3.
answered Dec 15, 2010 at 20:35
Karl KnechtelKarl Knechtel 59.1k9 gold badges86 silver badges130 bronze badges 3 Other approach:
or a one liner:
Thanks for the opportunity to think about this problem :) Cheers answered Dec 15, 2010 at 20:18
MorlockMorlock 6,60015 gold badges41 silver badges49 bronze badges
answered Dec 15, 2010 at 20:20
barti_ddubarti_ddu 9,9491 gold badge44 silver badges51 bronze badges You can save one write call with this:
When using 'r+', you would have to rewind the file after reading and before writing. answered Dec 15, 2010 at 20:04
infraredinfrared 3,4082 gold badges23 silver badges37 bronze badges 2 Here's a 3 liner that I think is clear and flexible. It uses the list.insert function, so if you truly want to prepend to the file use l.insert(0, 'insert_str'). When I actually did this for a Python Module I am developing, I used l.insert(1, 'insert_str') because I wanted to skip the '# -- coding: utf-8 --' string at line 0. Here is the code.
answered Jan 4, 2014 at 15:50
3 This does the job without reading the whole file into memory, though it may not work on Windows
answered Jun 1, 2012 at 12:48
eugeug 1,1181 gold badge17 silver badges25 bronze badges 3 One possibility is the following:
answered Dec 15, 2010 at 20:18
JooMingJooMing 9022 gold badges7 silver badges14 bronze badges 2 If you wish to prepend in the file after a specific text then you can use the function below.
So first you open the file, read it and save it all into one string. Then we try to find the character number in the string where the injection will happen. Then with a single write and some smart indexing of the string we can rewrite the whole file including the injected text now. answered Aug 27, 2015 at 13:55
PithikosPithikos 17.5k15 gold badges108 silver badges128 bronze badges Am I not seeing something or couldn't we just use a buffer large-enough to read-in the input file in parts (instead of the whole content) and with this buffer traverse the file while it is open and keep exchanging file<->buffer contents? This seems much more efficient (for big files especially) than reading the whole content in memory, modifying it in memory and writing it back to the same file or (even worse) a different one. Sorry that now I don't have time to implement a sample snippet, I'll get back to this later, but maybe you get the idea. answered Jun 14, 2019 at 7:33
Zuzu CorneliuZuzu Corneliu 1,4651 gold badge13 silver badges26 bronze badges As I suggested in this answer, you can do it using the following:
answered Jan 5 at 10:18
JD SolankiJD Solanki 6406 silver badges15 bronze badges If you rewrite it like this:
It's rather short and simple. For 'r+' the file needs to exist already. answered Mar 31 at 21:23
SpartanSpartan 4506 silver badges11 bronze badges this worked for me
answered Jun 30 at 16:19
Ali80Ali80 3,9291 gold badge31 silver badges26 bronze badges How do you write to a line in Python?To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.
How do you get the first character of a line in Python?Get the first character of a string in python
As indexing of characters in a string starts from 0, So to get the first character of a string pass the index position 0 in the [] operator i.e. It returned a copy of the first character in the string. You can use it to check its content or print it etc.
|