-3

I have a .txt file generated from a python script that has the text: what is sdf (just random text) and when I add other random characters to it, two of the same file show up in GitHub Desktop and through the git status command on Git Bash as such:

GitHub Desktop

Git Bash: git status command result

I think that with my old script, there was a file created with the path specified in the second modified file (in the Git Bash screenshot) but I don't know how to get rid of it, since it's now like a ghost to the current file and mimics its changes. This is the script currently, with the old path being that of the second file's:

basic_addition_file = open('src/answers/Mathematics/basic_addition.txt', 'w')
Daggerpov
  • 127
  • 1
  • 13
  • I don't understand your problem. What do you have? What did you expect? – Christoph Jul 28 '21 at 14:53
  • In my directory, there is just one file. However, when I go to GitHub Desktop/Git Bash, there's then two and I want to get rid of this extra one. – Daggerpov Jul 28 '21 at 14:56
  • I think that the duplicate file could be untracked, since it doesn't show up in my directory or repo on github.com so it's just in the version control and annoying me. @Christoph – Daggerpov Jul 28 '21 at 15:04
  • Have a look here https://stackoverflow.com/a/18982789/5784831 – Christoph Jul 28 '21 at 16:07

2 Answers2

0

I believe the error you are running into is caused by the fact that you are not using the with keyword as Python recommends, and my guess is that you are not closing the file resulting in the file not being completely written to the disk, ie your 'ghost'.

Give the Python documentation a read and I think you'll be able to solve your problem.

Jacob
  • 1,697
  • 8
  • 17
  • so, I do have a .close() command directly after my write command. Even after adding your suggested change: `with open('src/answers/Mathematics/basic_addition.txt', 'w') as f:` `f.write(f"What is {basic_addition_first} + {basic_addition_second}?: \ {str(basic_addition_answers).replace('[', '').replace(']', '')}")` `f.close()` and pushing this change through GitHub Desktop, the 'ghost' file is still marked as changed. @Christoph – Daggerpov Jul 28 '21 at 15:08
  • In that case check out [this](https://stackoverflow.com/a/64966/8700809) post about cleaning your untracked files and see if the ghost file comes back after you make your text file changes and running the script – Jacob Jul 28 '21 at 15:17
  • So, I actually went down this rabbit hole last night and eventually did delete this file but then it came back. Right now, these are the commands I ran and their results: git status (got the same result as the image in my post) git clean -fX (to clean out untracked files by deleting them) Removing .env Removing package-lock.json (failed to remove the "src\\answers\\Mathematics\\basic_addition.txt file) – Daggerpov Jul 28 '21 at 15:21
0

https://stackoverflow.com/a/18982789/13368695

This answer is what I was looking for, since I committed to the 'ghost' file by using an improper path with \ rather than / and that's an issue on Windows, specifically. So, with another Unix-based machine, I corrected this issue as this answer suggested.

Daggerpov
  • 127
  • 1
  • 13