Dec-30-2022, 08:35 AM
I have got a simple file read code and I need to understand why it works the way it does.
Also, running print(ReadFileContents(pth)[0]) gives me the first and only item of the list, 12/25/2022.
However running, print(ReadFileContents(pth)[:-1]), I get the undesirable result, an empty list, []. I understand this splicing to mean, start with the first item of the list and return up to the last item. Why then does it return an empty list?
Thank you for any clarification
def ReadFileContents(file_path): try: with open(file_path,'r', newline='') as f: contents = f.read().splitlines() return contents except Exception as e: print(f'{e} -- ReadFileContents')If I run it like this: print(ReadFileContents(pth)), I get the desired result, a list of the single date entry in the file: ['12/25/2022'].
Also, running print(ReadFileContents(pth)[0]) gives me the first and only item of the list, 12/25/2022.
However running, print(ReadFileContents(pth)[:-1]), I get the undesirable result, an empty list, []. I understand this splicing to mean, start with the first item of the list and return up to the last item. Why then does it return an empty list?
Thank you for any clarification