Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a file
#1
I have got a simple file read code and I need to understand why it works the way it does.

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
Reply
#2
In a list slice, such as mylist[a:b], the item at index b is not included in the result.
>>> mylist = ['a', 'b', 'c']
>>> mylist[0:1]
['a']
>>> mylist[0:-1]
['a', 'b']
>>> mylist[:]
['a', 'b', 'c']
JonWayn likes this post
Reply
#3
ninjad by Gribouillis Smile

Slice value at end is 'first to be excluded'. So you basically say: give me whole list except last item. As your list happens to have only one item (which is also a last item) you get the empty list.
JonWayn likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Dec-30-2022, 08:35 AM)JonWayn Wrote:
print(ReadFileContents(pth)[:-1]
If you want the last item, you should not use the colon, but:
print(ReadFileContents(pth)[-1]
JonWayn likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 655 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 930 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading Specific Rows In a CSV File finndude 3 998 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 902 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 997 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,838 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Reading .csv file doug2019 4 1,710 Apr-29-2022, 09:55 PM
Last Post: deanhystad
  Reading an Input File DaveG 1 1,260 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  Initializing, reading and updating a large JSON file medatib531 0 1,799 Mar-10-2022, 07:58 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020