0

I ran the following code in Python:

def translateDesc(fileName):
    textFile = open("fileName", "r")
    fileLines = textFile.readlines()
    return fileLines

And the function returns an empty string. However, when I ran the lines one by one in the iPython terminals, the function returned was not empty. How should I fix this issue?

2
  • 2
    Use the filename variable: textFile = open(fileName, "r") Commented Sep 27, 2020 at 16:10
  • And be more clear. readlines doesn't return strings. It returns a list of some number of strings. Commented Sep 27, 2020 at 16:12

2 Answers 2

1

readlines() returns a list of strings.If you want to return string use read()

def translateDesc(fileName):
    textFile = open(fileName, "r")
    fileLines = textFile.read()
    return fileLines
Sign up to request clarification or add additional context in comments.

Comments

0

please replace 2nd line with below: textFile = open(fileName, "r")

#we need to use the variable name in line 2 instead of string "fileName".

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.