Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

My first useful python program!

Name: Anonymous 2013-01-21 23:01

Good evening my good friends of /prog/! Thanks to your encouragement, I made my first useful python program today. Basically it generates an HTML script that displays every image in a directory. I'll post it in hopes that you can use it !~{^-^}~♥

#index generator for directory of images for HTML
import os
import fnmatch

FNL = os.listdir(".")

f = open('index.html','w')
f.write("<html><head><title>Index of...</title></head><body>")

#make a list of workable files in this
for FileName in FNL:
    if fnmatch.fnmatch(FileName, '*.jpg'):
        f.write("<img src=\""+FileName+"\"></img><br>")
    if fnmatch.fnmatch(FileName, '*.jpeg'):   
        f.write("<img src=\""+FileName+"\"></img><br>")
    if fnmatch.fnmatch(FileName, '*.gif'):   
        f.write("<img src=\""+FileName+"\"></img><br>")
    if fnmatch.fnmatch(FileName, '*.png'):   
        f.write("<img src=\""+FileName+"\"></img><br>")
    if fnmatch.fnmatch(FileName, '*.bmp'):   
        f.write("<img src=\""+FileName+"\"></img><br>")
       
#close up by writing the ending tags
f.write("</body></html>")

Name: Anonymous 2013-01-22 1:08

Well, that's one way to do it.

You don't need the closing </img> tag. All browsers treat img as a closed tag. You can write <img .../> if it makes you feel better.

There's a lot of duplicate code there. That entire for loop can be written:

PATTERNS = ['*.jpg', '*.jpeg', '*.gif', '*.png', '*.bmp']
for FileName in FNL:
    for FilePattern in PATTERNS:
        if fnmatch.fnmatch(FileName, FilePattern):
            f.write("<img src=\""+FileName+"\"></img><br>")


Then if you need to change the write statement, you only need to do it once, and adding new formats is as simple as:

PATTERNS = ['*.jpg', '*.jpeg', '*.gif', '*.png', '*.bmp', '*.tiff']

Name: Anonymous 2013-01-22 1:55

Name: Anonymous 2013-01-22 1:55

Name: Anonymous 2013-01-22 1:56

Name: Anonymous 2013-01-22 1:56

Name: Anonymous 2013-01-22 1:57

Name: Anonymous 2013-01-22 2:16

Ha! Try doing that in LISP, faggots.

Name: Anonymous 2013-01-22 2:36

How is that useful? Also, it's written in Python and even then is written like shit. You never even checked to see if the file opened successfully. I'll give your code a 1/10.

Name: Anonymous 2013-01-22 2:46

>>9
do you need to check that if the file is being created?

Name: Anonymous 2013-01-22 2:57

>>10
Yes. Opening a file for writing can fail and you need to check for it. That entire program would be much better suited for Perl anyway.

Name: Anonymous 2013-01-22 2:58

>>10
Yeah, the user may not have permission to create a file in the current directory.

Name: Anonymous 2013-01-22 3:03

>>11,12
That's what you chose to point out? This crap just assumes that the filesystem uses "." for the working directory.

Name: Anonymous 2013-01-22 3:05

Preferred method for opening file is with open('file.txt', 'r') as f, because it closes file automatically. f=open is gay bullshit

Name: Anonymous 2013-01-22 3:11

>>14
Not only is "w" needed here, but >>1 does not close this file. There is a reason for this. I'll let you figure it out though, as it is best to not spoonfeed those who have not achieved Satori yet.

Name: Anonymous 2013-01-22 3:11

>>14
What about '</html>' et al?

Name: Anonymous 2013-01-22 3:30

>>15
SPOONFEED MY ANUS

Name: Anonymous 2013-01-22 4:03

>>2
YHBT

Name: Anonymous 2013-01-22 8:17

THE PLEASURE OF BEING FORCIBLY INDENTATED INSIDE

Name: sage 2013-01-22 8:22

sage this shit.

Name: SAGE MY ANUS 2013-01-22 10:12

SAGE MY ANUS

Name: /prog/ CHALLENGE 2013-01-22 10:17

Rewrite >>1's code using xml.dom.minidom for proper output generation.

Deadline is never, it's too hard for anyone here

Name: Anonymous 2013-01-22 11:34

XML is shit, HTML is shit and nobody should care about this.

New challenge: Write a browser that interprets S-expressions and Scheme instead of HTML and Javashit. You can redefine the standards if you want. Actually, do it, W3C is a bunch of kikes.

Name: Anonymous 2013-01-22 13:15

Please read the fucking PEP-0008 and adhere to it.

Name: Anonymous 2013-01-22 14:32

>>13
os.listdir is only available on Windows and Unix. It's a warranted assumption.

Don't change these.
Name: Email:
Entire Thread Thread List