I need help with this

This work is supposed to test you on reading and writing to a text file and working with JSON objects.
Hint: Work on the methods in the order they are found in the documentation below.

def read_last_line(file_path: str)->str:
“”"
Reads the last line of the file at the specified file path. The function returns the last line of the file as a
string, if the file is empty it will return an empty string (“”).
:param file_path: A path to a file
:return: The last line of the file
“”"

def read(file_path: str)->str:
“”"
Reads the entire file at the specified file path. The function returns all the text in the file as a string, if
the file is empty it will return an empty string (“”).
:param file_path: A path to a file
:return: All the text in the file
“”"

def write(file_path:str, text:str=‘’):
“”"
Clears the file at the specified file path then writes the specified line to the file. If the function is invoked
without a line parameter or the line variable is None nothing is written to the file but the file should still be
cleared. If the file does not exist a new file is created, regardless if a text is specified.
:param file_path: A path to a file
:param line: None
:return:
“”"

def write_last_line(file_path:str, text:str=‘’):
“”"
Writes the specified line to the file as the last line. If the text parameter does not start with a new line
character this adds a new line character to the text parameter so that the text is written on the next line of
the file. If the function is invoked without a line parameter or the line variable is None nothing is written to
the file. If the file does not exist a new file is created, regardless if a text is specified.
:param file_path: A path to a file
:param text: The last line to be written to the file.
:return: None
“”"

def clear(file_path: str):
“”"
Clears the file at the specified file path.
:param file_path: A path to a file
:return: None
“”"

def delete_last_line(file_path: str) → str:
“”"
Removes the last line in the file at the specified file path. Then it saves the new value with the last line
removed to the file. Finally it returns the deleted last line. If the file has nothing in it an empty
string (“”) is returned.
:param file_path: A path to file
:return: The text in the file with the last line removed
“”"

def swap_value(file_path: str, key: str, replacement):
“”"
This function will be given a file path and a key. The file that the file path points to contains a json object.
This function should, load the data in the file. Then replace the value associated with the key with replacement
value. Then it should overwrite the current data in the file with the new changes. Finally it should return the
old value.

:param file_path: A path to file
:param key: A key value in the JSON object saved in the file
:param replacement: The new value that the key will be mapped to
:return: The old value that the key used to be mapped to
"""

def update_transactions(file_path: str, transaction_list: list):
“”"
You are part of a team tasked to create an application that helps bankers by saving their transactions for them.
Your job within the project is to implement the feature that actually saves the transactions to the hard drive.
The scope of your task is that you will be given a file path to where the data should be written and a list of
bank transaction objects to be written to the file. The file will contain a JSON array of transactions that the
banker previously saved. The bankers at this bank are silly sometimes and they make duplicate transactions, your
function should remove the duplicate transactions in the transaction_list and update the existing transaction list
saved in the file with the new transactions. Make sure when you are preforming the update of the new transactions
that you overwrite old transactions (transactions that already exist in the file) with duplicate new transactions
(transactions that are found in the transaction_list). You will know that a transaction is a duplicate if it has
the same ID.

In the end the file should contain a JSON list with transaction Objects that have been updated with the new
information from the transaction_list. The JSON list should contain no transactions with the same ID.

The transaction object referred to here in is outlined below. In actual testing of your code the professors
transaction object will be used, which will have all the documented functionality.

class Transaction:

    def __init__(self, id:int, type:str, amount:float):
        self.id:int = id
        self.type:str = type
        self.amount:float = amount

    def __str__(self):
        return f'Transaction[{self.id}] {self.type} ${self.amount}'

    def __hash__(self):
        return hash(self.id)

    def __eq__(self, other):
        return hasattr(other,'id') and other.id == self.id

:param file_path: A path to file blank file that holds a JSON list of existing transaction and where the new
 transaction data should be written
:param transaction_list: A list of transaction
:return: None
"""

What is your issue/question?
Please provide a link to the lesson.

import json

def read_last_line(file_path: str)->str:

with open(file_path, 'r') as myfile:
  file_content = myfile.read().splitlines()
if len(file_content) > 1:
  return file_content[-1]
else:
  return ""

def read(file_path: str)->str:

with open(file_path, 'r') as myfile:
  file_content= myfile.read()
if len(file_content) > 1:
  return file_content
else:
  return ""

def write(file_path:str, text:str=‘’):

with open(file_path, 'a+') as myfile:
  myfile.write(text)

def write_last_line(file_path:str, text:str=‘’):

with open(file_path, 'a+') as myfile:
  myfile.write('\n' + text)

def clear(file_path: str):

json.remove(file_path)
print("The file has been removed.")

def delete_last_line(file_path: str) → str:

with open(file_path, "r") as file:
  data = file.read().splitlines()
  if len(data) > 0:
    with open(file_path, "w") as myfile:
      for line in data[:-1]:
        myfile.write(line + "\n") 
    return data[-1]
  else:
    return ""

def swap_value(file_path: str, key: str, replacement):

with open(file_path, 'r') as myfile:
  file_content = (myfile.read())
  old_value = file_content[key]
  file_content[key] = replacement
  file_content = json.dumps(file_content, indent = 4)
  print("File after swapping the values is below: ", '\n', file_content)
with open(file_path, 'w') as myfile:
  myfile.write(str(file_content))
  return old_value

def update_transactions(file_path: str, transaction_list: list):
with open(file_path, ‘r’) as myfile:
transaction = (myfile.read())
print(“Old transaction list: {}”.format(transaction))

class Transaction:
def init(self, id:int, type:str, amount:float):
self.id:int = id
self.type:str = type
self.amount:float = amount

def __str__(self):
  return f'Transaction[{self.id}] {self.type} ${self.amount}'

def __hash__(self):
  return hash(self.id)

def __eq__(self, other):
  return hasattr(other,'id') and other.id == self.id

def check_duplicate_enteries(self, TransactionList):
  for i, j, k in TransactionList:
    if i == self.id:
      TransactionList[j] = self.type
      TransactionList[k] = self.amount

def add_new_entry(self):
  entry = {}
  entry['id'] = self.id
  entry['type'] = self.type
  entry['amount'] = self.amount
  return entry

new_transaction = Transaction(*transaction_list)
entry = new_transaction.add_new_entry()
for trans in transaction[“TransactionList”]:
if trans[“id”] == entry[‘id’]:
transaction[“TransactionList”].remove(trans)
print(“You were trying to add duplicate record. Duplication is removed.”)
(transaction[“TransactionList”]).append(entry)

with open(file_path, ‘w’) as myfile:
newEntry = json.dumps(transaction, indent = 4)
print(“Transaction list after updation: {}”.format(newEntry))
myfile.write(str(newEntry))

trans,json
{
“TransactionList”: [
{
“id”: 1234,
“typeOfTransaction”: “check”,
“amount”: 1000
},
{
“id”: 4567,
“typeOfTransaction”: “withdrawals”,
“amount”: 1500
},
{
“id”: 8910,
“typeOfTransaction”: “check”,
“amount”: 5000
},
{
“id”: 1112,
“type”: “withdrawals”,
“amount”: 9000
}
]
}

myfile.txt

Hello
My name is name
I am a student
Bye from name

dictionary_file.txt

{
“name”: “name”,
“role”: “student”,
“school”: “school”
}

is this right? for my assignment

We cannot help you with any school/class assignments.

https://discuss.codecademy.com/guidelines

oh ok, thank you for letting me know.