Goal
Goal

Reputation: 87

How can I pass a existed txt file into a function?

So I already have a text.txt file contain with something like:

This is my file
for my exercise
and i need pass this file
into a function

And at first i need to pass it into a function we can say like functionForFile() so i can working on this file. But I have no idea how to do it. Please give me some advices

Upvotes: 0

Views: 59

Answers (3)

kederrac
kederrac

Reputation: 17322

you can pass only the path and then play with the object file inside the function:

functionForFile(file_path)

if you want to pass only the object file you can do:

of = open(<file_path>, 'r')
functionForFile(of)

Upvotes: 0

publicdomain
publicdomain

Reputation: 1722

Do

f = open("your-text-file.txt")
content = f.read()
f.close()

that way you get the content of the file

Upvotes: 0

schlodinger
schlodinger

Reputation: 559

You can use something like this:

with open("text.txt", "r") as f:
    text = f.read()

functionForFile(text)

Upvotes: 3

Related Questions