Bayram
Bayram

Reputation: 73

can't read from txt file python

def add(num1,num2):
    print(num1+num2)


try:
    filename = input()
    f = open(filename,"r")
    readFile = f.readlines()
    f.close()
    for line in readFile:
        exec(line)

except FileNotFoundError:
    print("Failed to open",filename)

I have a function called add() given a text file with values like this add(12345667890123456789,8765432109876543210). I need to open text file then execute that function. Why doesn't the code above open the file?

Upvotes: 0

Views: 1142

Answers (1)

Daniel Lima
Daniel Lima

Reputation: 995

Try the following:

with open("C:\\Users\\Admin\\Desktop\\tc6.txt", 'r') as f:
    readFile = f.readlines()

Upvotes: 2

Related Questions