How to pass a variable as a string as well as a file in python function?

I need to pass an argument that has to read a file as well as a string in the python function. Here, it works fine when I pass a filename as variable, but the elif block doesn’t work here which has to read a string. Please help

     def test_save(self, test_data):
          if Path(test_data).is_file():
                with open(test_data, 'r') as f:
          elif type(test_data) is str:
                result= test_data
          data = {
                "president": {
                 "details": result
     test_data_String_value = "<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>"            } 






  • replacing type(test_data) is str with isinstance(test_data, str)
  • adding a return data after the if / elif block

With the string you provide, test_save raises an OSError, which you might want to catch with a try / except statement.