0

For instance, the following test.py script can be displayed at ../cgi-bin/test.py, but is there a way I could display the same output from a text file (like test.txt) instead of text.py, so that the url would be something like ../text.txt?

--- test.py ---

def printxt():
  print "Content-Type: text/plain"
  print """my text here..."""

3 Answers 3

1

If you point your browser to www.yoursite.com/yourtext.txt, you'll discover that most browsers are fully capable of displaying text files without any additional code.

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

Comments

0

You appear to be asking if web servers can serve, and web browsers display text files. The answer is yes. Put the text file underneath DOCUMENTROOT and it's all good.

Or did I misunderstand the question?

Comments

0

If you want to use Python to do this in the context of a larger program (the example you just gave would be useless if that's all that you wanted it to do), you can simply use the standard:

file = open("filename.txt", "r")
for line in file:
  print line

You already know about the Content-type line which of course would be the same.

If the file is small enough to be read into memory all at once without causing problems, you can use "print file.read()" in order to have file.read() read the entire file as a single string, then print that out.

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.