New to Python - Easy way to open a text file

M

Max Steel

Hey gang, I'm new to python coding. I'm trying to find the simplest way to
open a text file (on the same server) and display it's content.

The text file is plain text (no markup language of any kind).

The filename gets found and placed into a variable named [plainfiles.href]

I'm using python to find the text files, and a .ezt template to display the
list of the files. But I can't figure out how to read the contents of the
file, and display it within the same template.

Any help is appreciated.

Thanks in advance,
..\\axSteel
 
K

kyosohma

Hey gang, I'm new to python coding. I'm trying to find the simplest way to
open a text file (on the same server) and display it's content.

The text file is plain text (no markup language of any kind).

The filename gets found and placed into a variable named [plainfiles.href]

I'm using python to find the text files, and a .ezt template to display the
list of the files. But I can't figure out how to read the contents of the
file, and display it within the same template.

Any help is appreciated.

Thanks in advance,
.\\axSteel

I'm not familiar with ezt formats, however reading a text file is a
breeze.

f = open(r'pathToFile')
while True:
line = f.readline()
if not line: break
# do something with the line of text such as print it.

f.close()


I suppose you could write it to .ezt? Maybe you need to format each
line before inserting it. I hope this points you in the right
direction anyway.

Mike
 
S

skip

Max> Hey gang, I'm new to python coding. I'm trying to find the
Max> simplest way to open a text file (on the same server) and display
Max> it's content.

Try the open() builtin function:

f = open(plainfiles.href, "r")
print f.read()

Skip
 
M

Marc 'BlackJack' Rintsch

I'm not familiar with ezt formats, however reading a text file is a
breeze.

This sentence doesn't match the code that follow. It's really simpler
than that ``while`` loop.
f = open(r'pathToFile')
while True:
line = f.readline()
if not line: break
# do something with the line of text such as print it.

f.close()

f = open(r'pathToFile)
for line in f:
# do something with the line of text such as print it.
f.close()

Ciao,
Marc 'BlackJack' Rintsch
 
M

Max Steel

Hmm.. maybe it'd help if I explained that I'm playing with edna
http://edna.sourceforge.net/

I'll play with this simple code:
f = open(r'pathToFile)
for line in f:
# do something with the line of text such as print it.
f.close()

and this one from Skip:
f = open(plainfiles.href, "r")
print f.read()

and see if I can make a go of it.. I think it's the template portion that is
mixing me up.. the template is what's used to create the look of the html on
the edna server.

Thanks to all who replied so quickly, much appreciated!
..\\ax
 
K

kyosohma

This sentence doesn't match the code that follow. It's really simpler
than that ``while`` loop.



f = open(r'pathToFile)
for line in f:
# do something with the line of text such as print it.
f.close()

Ciao,
Marc 'BlackJack' Rintsch

Yes, the "while" is lame, I admit it. But sometimes you just gotta go
old school!

Mike
 
M

Michael Tobis

I think it's pretty clear that we aren't understanding what you mean
by "open a text file and disply its content".

I conclude that by "edna" you mean this thing: http://edna.sourceforge.net/

I suspect you are not asking a Python question at all.

Did you try opening

file:///<path-to-edna>/edna-0.6/templates/default.ezt

in your browser? This will show you the design of your (unpopulated)
template rendered in your browser.

Notes:

1) yes you do need three slashes after "file:" for some reason I've
never comprehended
2) you will need to replace <path to browser> with your local system
path to the edna directory. (In firefox you can, alternatively, browse
to the file from File->open; Safari will balk at the ezt extension; I
don't know what other browsers will do.)

If this is more like what your are after, consider that your question
was misleading (and entirely unrelated to Python). The edna miling
list is here:
http://www.lyra.org/mailman/listinfo/edna

hth
mt
 
M

Max Steel

Nope. that's not what i mean...

Here's a quick run down:

edna, is a python mp3 server. (.py)

What it does is searches directories for:
1) mp3s
2) jpgs
3) mp3us
4) txt files
5) other folders

What I'd like it to do, is upon finding a folder, with a .txt file in it (in
my case is probably an album review from AMG), is to take the contents of
that text file and display it on the same page as the album, the artwork
etc..

I think the gang that has helped me so far, is pointing me in the right
direction. I just need to figure out how this template is used.

I'll have some time on sunday to play with the "simple" code that they've
provided. I will post my results.

Thanks again to all who has helped me thus far,
..\\ax
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top