0

How can I open a file once I am inside Python, that is, once I have typed "python" in the terminal? I know how to open a file by typing something similar to the following in a script, and then running it:

from sys import argv
script, filename = argv
txt = open(filename)
print txt.read()

But I have no idea how to do it once I'm inside the Python interpreter. I've tried to type open (file.txt) and also open ("file.txt"), but I get a long error message either way.

Which is the correct way to do this?

1 Answer 1

1

You have to add a mode to the call txt = open('filename.txt', 'r') if you want to read (or w/a for writing or appending). I just tried it, it works :)

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.