1

I have python 2.7 code with creates a builtin file object which return me a file object <type 'file'>

 file(os.path.join('/tmp/test/', 'config.ini'))

Same code i changed in python 3.7 as below it returns <class '_io.TextIOWrapper'>

 open(os.path.join('/tmp/test/', 'config.ini'))

How can i get a file object type in python 3

1

1 Answer 1

1

You can use them the exact same way.

file = open(os.path.join("/tmp/test/", "config.ini"))
print(file.read())  # Will print file contents

Or use pathlib:

from pathlib import Path
file = Path("/tmp/test") / "config.ini"
print(file.read_text())  # will do the same

You're not looking for a file object. You're looking for an object that'll let you read and write files.

Sign up to request clarification or add additional context in comments.

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.