Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a beginner in Python. I want to add first row in a text file that looks like this:
1 2 3
4 5 6
7 8 9 

so that the output after adding the desired addition is:
x y z
1 2 3
4 5 6
7 8 9

How can I do that using Python?

What I have tried:

I have tried
Python
df=pd.read_table('file.txt, header=['x', 'y', 'z']

but it didn't work.
Posted
Updated 26-Feb-22 3:26am
v4
Comments
Andre Oosthuizen 24-Feb-22 16:35pm    
Your question is very vague, I have changed it to what I think is what you wanted to ask. If not, please change the question by using the "Improve Question" link above.

1 solution

To answer your question, see THIS link.

Append data to a file as a new line in Python.
Solution for this is a little tricky here. Let's start with the basic approach and then we will discuss drawbacks in it and see how to improve it.

Basic approach:

Open the file in append mode (‘a’). Write cursor points to the end of file.
Append ‘\n’ at the end of the file using write() function.
Append the given line to the file using write() function.
Close the file.
Well, this approach works fine if our file already exists and already has some data in it. But if the file doesn’t exist or file is empty, then this approach will fail because contents of the file will be like this.

……………………..

New added Line:

It first writes an empty line and then writes our line. But in this case, only appending a line was fine, we don’t need to write ‘\n’ before that.

So, our final approach should be like this:

Open the file in append & read mode (‘a+’). Both read & write cursor points to the end of the file.
Move read cursor to the start of the file.
Read some text from the file and check if the file is empty or not.
If the file is not empty, then append ‘\n’ at the end of the file using write() function.
Append a given line to the file using write() function.
Close the file.
This solution will work fine in both scenarios. Let’s use this solution to append a newline at the end of the file.

Suppose we have a file ‘sample2.txt’ with the following contents:

Hello this is a sample file
It contains sample text
This is the end of file

Append new line to the file:
Python
# Open the file in append & read mode ('a+')
with open("sample2.txt", "a+") as <code>file_object</code>:
<pre lang="Python">    # Move read cursor to the start of file.
    file_object.seek(0)
    # If file is not empty then append '\n'
    data = file_object.read(100)
    if len(data) > 0 :
        file_object.write("\n")
    # Append text at the end of file
    file_object.write("x y z")

Contents of the file ‘sample2.txt’ now,
Hello this is a sample file
It contains sample text
This is the end of file
hello hi
 
Share this answer
 
v3
Comments
Youssef Hany 2021 24-Feb-22 16:43pm    
I found that solution but I want the added row to be the first not the last
Maciej Los 25-Feb-22 2:23am    
So... Open new text file, add "a header", read existing file to the end and write its content into new file, close new file and override existing file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900