abooli
abooli

Reputation: 11

Python readlines() returns empty string

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?

Upvotes: 0

Views: 1267

Answers (2)

Gnanavel
Gnanavel

Reputation: 665

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

Upvotes: 1

Shree H
Shree H

Reputation: 16

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

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

Upvotes: 0

Related Questions