1

I have declared an empty list. I want to store in that one some strings from a text file. If I am doing without creating TXT_to_PList it is working smoothly. But using TXT_to_PList length of deTrimis array will be 0. Why?

deTrimis = []

def TXT_to_PList(fileName,array):
    with open(fileName) as f:
        array = f.read().splitlines()
TXT_to_PList('strings.txt',deTrimis)
print (len(deTrimis))
Viorel
  • 39
  • 7

2 Answers2

1

You don't need that empty list at all:

def TXT_to_PList(fileName):
    with open(fileName) as f:
        return f.read().splitlines()

deTrimis = TXT_to_PList('strings.txt')
Peter
  • 10,959
  • 2
  • 30
  • 47
0

You can't modify function argument by assignment, change it to:

array.extend(f.read().splitlines())

see more info here: Why can a function modify some arguments as perceived by the caller, but not others?

RafalS
  • 5,834
  • 1
  • 20
  • 25