-4

I actively trade stocks and I am trying to manipulate a file in Python that will act as a way to check my current holdings.

Upon opening, it will read the contents of the file to see if I have any holdings. If I enter something, I will enter the ticker and append the file. How would I make it so that if I wanted to exit something, I can type the ticker and it would delete that string from the file?

Truncate won't work because if I have more than one position on, the entire file will be wiped. I have tried many people's suggestions on this topic but none have worked. Can anyone put forth an easy way to exit the holding in the file based upon my inputs? Thanks in advance!

  • 2
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a coding or tutorial service. – Prune Dec 22 '16 at 01:57
  • Welcome to Stack Overflow! Please provide some extra information regarding this issue and the code that you have already tried. – Mark Skelton Dec 22 '16 at 01:57
  • Read the whole file into memory (assuming it's not huge), manipulate the data the way you want and then re-write the file with the updated data. – Nir Alfasi Dec 22 '16 at 02:00
  • @MarkyPython well I do not know a way to delete a word by string inside a file. I was hoping someone would post an example – user7328255 Dec 22 '16 at 02:00
  • The goal of Stack Overflow is for us to help you solve issues with code that you have wrote. Try looking at some python tutorials for working with files like this one: https://www.tutorialspoint.com/python/python_files_io.htm – Mark Skelton Dec 22 '16 at 02:02
  • Possible duplicate of [How do I modify a text file in Python?](http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python) – Nir Alfasi Dec 22 '16 at 02:06

1 Answers1

0

The first answer to the following post provided exactly what I was looking for... Deleting a specific line in a file (python)