# Appending a file

**URL:** <https://discuss.python.org/t/appending-a-file/33540>\
**Category:** Python Help\
**Tags:** help, file-handling\
**Created:** [September 11, 2023, 4:02pm UTC](https://discuss.python.org/t/appending-a-file/33540 "2023-09-11T16:02:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eyalvr12](https://avatars.discourse-cdn.com/v4/letter/e/c5a1d2/32.png) [@Eyalvr12](https://discuss.python.org/u/Eyalvr12)\
**Post date:** [September 11, 2023, 4:02pm UTC](https://discuss.python.org/t/appending-a-file/33540/1 "2023-09-11T16:02:01Z")

</div>

```auto
check2 = open(r'C:\Users\DELL\Desktop\mathF.txt', 'a')
        check2.write(' = ' + str(result))
        check2 = open(r'C:\Users\DELL\Desktop\mathF.txt', 'r')
        print(check2.read())

```

I wanna add the ‘=’ + result to the file at the end of the current line. But it goes to a new line and prints it there.  
Why does this happen?  
thanks

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 11, 2023, 4:18pm UTC](https://discuss.python.org/t/appending-a-file/33540/2 "2023-09-11T16:18:01Z")

</div>

I suspect that the file already ends in a newline character (or `\r\n` since you’re on Windows), so you’re appending after that. It can difficult to see this by viewing the file unless you have an editor that will show you special characters.

If you read the original file and `print` the result, does the output have a blank line? That would be a sign that there’s a newline at the end (and then `print` is adding another one, causing a blank line in the output)

It can be tricky to remove because some text editors will add a newline when you save, but it’s possible to do it that way. Or if you created the file in Python, you can make sure that you don’t end on a newline.

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [September 11, 2023, 4:56pm UTC](https://discuss.python.org/t/appending-a-file/33540/3 "2023-09-11T16:56:20Z")

</div>

Maybe moving file pointer to the end of a file using `.seek(0, 2)` and write there.

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [September 11, 2023, 5:08pm UTC](https://discuss.python.org/t/appending-a-file/33540/4 "2023-09-11T17:08:57Z")

</div>

@Eyalvr12 -  
If I may make a suggestion - when opening files it’s generally better to get into the habit of using a context manager:

```auto
with open(path, "r") as f:
    s = f.read()
s = s.rstrip('\n') # strips all new lines at the end of the text
# s.rstrip() would strip all whitespace, which is perhaps what you want
s = f"{s} = {result}\n"
with open(path, "w+") as f:
    f.write(s)

```

---

<div class="post-metadata">

**Author:** ![Eyalvr12](https://avatars.discourse-cdn.com/v4/letter/e/c5a1d2/32.png) [@Eyalvr12](https://discuss.python.org/u/Eyalvr12)\
**Post date:** [September 12, 2023, 3:31pm UTC](https://discuss.python.org/t/appending-a-file/33540/5 "2023-09-12T15:31:28Z")

</div>

I tried to just append it on a new empty file, does this problem still apply because it still won’t work:

```auto
check2 = open(r'C:\Users\DELL\Desktop\Empty.txt', 'a')
        line+= '=' + str(result)
        check2.write(line)
        check2 = open(r'C:\Users\DELL\Desktop\Empty.txt', 'r')
        print(check2.read())

```

Im just appending the line to this new file but for some reason it seperates the first part - then new line - then the second part

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 12, 2023, 3:33pm UTC](https://discuss.python.org/t/appending-a-file/33540/6 "2023-09-12T15:33:34Z")

</div>

What does the value of `line` look like before you run this? If you read it from the original file you have the same issue–there’s a newline at the end.

---

<div class="post-metadata">

**Author:** ![Eyalvr12](https://avatars.discourse-cdn.com/v4/letter/e/c5a1d2/32.png) [@Eyalvr12](https://discuss.python.org/u/Eyalvr12)\
**Post date:** [September 12, 2023, 4:05pm UTC](https://discuss.python.org/t/appending-a-file/33540/7 "2023-09-12T16:05:26Z")

</div>

The value of line before I append it the the new file still has that weird newline:

```auto
#print(line)
46 + 19
=65
#print(check2.read())
46 + 19
=65

```

now, what changes the value of line is when I do the += ‘=’ + str(result).  
And yes ,I read the line from the original file  
Is there anyway to go around that?

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 12, 2023, 4:21pm UTC](https://discuss.python.org/t/appending-a-file/33540/8 "2023-09-12T16:21:39Z")

</div>

Can you print `line` _before_ you append to it with `+= "=" + str(result)` ?

---

<div class="post-metadata">

**Author:** ![Eyalvr12](https://avatars.discourse-cdn.com/v4/letter/e/c5a1d2/32.png) [@Eyalvr12](https://discuss.python.org/u/Eyalvr12)\
**Post date:** [September 15, 2023, 7:55am UTC](https://discuss.python.org/t/appending-a-file/33540/9 "2023-09-15T07:55:24Z")

</div>

> [@Eyalvr12](#):
>
> `46 + 19`

It prints that part

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [September 15, 2023, 8:27am UTC](https://discuss.python.org/t/appending-a-file/33540/10 "2023-09-15T08:27:43Z")

</div>

If file have only one row `46 + 19` (or you want to add to the last row) then this should work: open file, go to end, go one character before end, write:

```python
with open("my_file.txt", "a") as f:
    f.seek(0, 2)
    f.seek(f.tell() - 1)
    f.write(" = 65")

# 46 + 19 = 65

```

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 15, 2023, 1:14pm UTC](https://discuss.python.org/t/appending-a-file/33540/11 "2023-09-15T13:14:27Z")

</div>

I was trying to about about the whitespace at the end of line, but maybe this is the wrong way to do that.

```auto
>>> print(line) # is it this?
46 + 19
>>> print(line) # or this
46 + 19

>>> # ^ note the blank line before the prompt

```

A simpler way to check is to print `len(line)` and/or `line.endswith("\n")`. If the length is 8, there’s an extra character in there that you don’t see (whitespace at the end).

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [September 15, 2023, 4:56pm UTC](https://discuss.python.org/t/appending-a-file/33540/12 "2023-09-15T16:56:49Z")

</div>

> [@aivarpaalberg](#):
>
> If file have only one row `46 + 19` (or you want to add to the last row) then this should work: open file, go to end, go one character before end, write:
> 
> ```auto
> with open("my_file.txt", "a") as f:
> f.seek(0, 2)
> f.seek(f.tell() - 1)
> f.write(" = 65")
> 
> # 46 + 19 = 65
> 
> ```

That won’t work (at least not on all OS’s - for instance it doesn’t work on Darwin). If you open the file in ‘a’ mode it may still append at the end and not overwrite, despite the seek position. One way to make this work is the code I posted earlier. Another way is Aivar’s code if you replace ‘a’ by ‘r+’.

To check the original file (and the modified file), you could use an editor that makes white-space and control chars visible, or do something like:

```auto
hexdump -C my_file.txt

```

---

<div class="post-metadata">

**Author:** ![Eyalvr12](https://avatars.discourse-cdn.com/v4/letter/e/c5a1d2/32.png) [@Eyalvr12](https://discuss.python.org/u/Eyalvr12)\
**Post date:** [September 15, 2023, 6:50pm UTC](https://discuss.python.org/t/appending-a-file/33540/13 "2023-09-15T18:50:01Z")

</div>

Will check.  
Thx

---

<div class="post-metadata">

**Author:** ![kartweb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kartweb/32/30267_2.png) [@kartweb](https://discuss.python.org/u/kartweb)\
**Post date:** [August 25, 2025, 8:54am UTC](https://discuss.python.org/t/appending-a-file/33540/14 "2025-08-25T08:54:26Z")

</div>

I had a similar issue and this solved it thanks!

---

<div class="post-metadata">

**Author:** ![DivyanshuSinha136](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/divyanshusinha136/32/29689_2.png) [@DivyanshuSinha136](https://discuss.python.org/u/DivyanshuSinha136)\
**Post date:** [August 25, 2025, 10:45am UTC](https://discuss.python.org/t/appending-a-file/33540/15 "2025-08-25T10:45:41Z")

</div>

Hey, this happens because when you open a file in `'a'` mode, Python **always moves the pointer to the end of the file** , but if the file already ends with a newline, your write will naturally start on a new line.

If you want to **add `= result` at the end of the current last line** , you can do something like this

```python
with open(r'C:\Users\DELL\Desktop\mathF.txt', 'rb+') as check2:
    check2.seek(-1, 2) # go to last byte
    if check2.read(1) == b'\n': 
        check2.seek(-1, 2)
        check2.truncate() # remove it
    check2.write(b' = ' + str(result).encode()) 

with open(r'C:\Users\DELL\Desktop\mathF.txt', 'r') as check2:
    print(check2.read())

```

This will **append `= result` at the end of the last line** without going to a new line.
