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))
1
  • Which version of Python are you actually interested in? Commented Apr 14, 2020 at 16:42

2 Answers 2

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')
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I needed. Thank you so much!
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.