Sunday, January 18, 2015

โปรแกรม Python สร้าง ไฟล์ HTML แสดงไดเร็คทอรี่ โดยแสดงผลผ่าน Browser

สร้างไฟล์ directory.py


Code Python:
#!/usr/bin/python
import os,webbrowser

f=""
sums = 0
num = 0
for subdir, dirs, files in os.walk('./'):
        for file in files:
        f += str(file)+""" : """+str(os.path.getsize(file))+" Bytes<br><br>"
        sums +=int(str(os.path.getsize(file)))
        num +=1
            print file
        print os.path.getsize(file)
        print sums

html="""<html> <body bgcolor='gray'> <center> <table border="1">
        <head>
            <title>Directory</title>
        </head>
        <body>
        <font size =10>
        <b>Directory File</b><br>
  
    <font size =7>  
    all file"""+str(num)+"""<br>

    <font size =5>
    """ +f+"""<br>
  
    <font size =7>
    Total Size : """+str(sums)+""" Bytes
        </font>
        </body>
        </html>"""
print html
python_html = open("directory.html", "w+")
python_html.write(html)
python_html.close()
webbrowser.open("./directory.html")



Run ไฟล์ directory.py


 Run ไฟล์ python-html.py โดยใช้คำสั่ง python directory.py

output:
test-html.hyml
1311
1311
python-html.py~
1839
3150
myprofile.html
1315
4465
python-html.py
1839
6304
pyhthon-html.py~
1848
8152
directory.html
586
8738
directory.py
864
9602
hello.html
19
9621
hello.py
27
9648
<html> <body bgcolor='gray'> <center> <table border="1">
        <head>
            <title>Directory</title>
        </head>
        <body>
        <font size =10>
        <b>Directory File</b><br>
  
    <font size =7>  
    all file9<br>

    <font size =5>
    test-html.hyml : 1311 Bytes<br><br>python-html.py~ : 1839 Bytes<br><br>myprofile.html : 1315 Bytes<br><br>python-html.py : 1839 Bytes<br><br>pyhthon-html.py~ : 1848 Bytes<br><br>directory.html : 586 Bytes<br><br>directory.py : 864 Bytes<br><br>hello.html : 19 Bytes<br><br>hello.py : 27 Bytes<br><br><br>
  
    <font size =7>
    Total Size : 9648 Bytes
        </font>
        </body>
        </html>
ubuntu@ubuntu:~/Desktop/building$
(process:4938): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed
 
แสดงผลบน Browser



output ไฟล์ directory.html



<html> <body bgcolor='gray'> <center> <table border="1">
        <head>
            <title>Directory</title>
        </head>
        <body>
        <font size =10>
        <b>Directory File</b><br>
  
    <font size =7>  
    all file9<br>

    <font size =5>
    test-html.hyml : 1311 Bytes<br><br>python-html.py~ : 1839 Bytes<br><br>myprofile.html : 1315 Bytes<br><br>python-html.py : 1839 Bytes<br><br>pyhthon-html.py~ : 1848 Bytes<br><br>directory.html : 586 Bytes<br><br>directory.py : 864 Bytes<br><br>hello.html : 19 Bytes<br><br>hello.py : 27 Bytes<br><br><br>
  
    <font size =7>
    Total Size : 9648 Bytes
        </font>
        </body>
        </html>


No comments:

Post a Comment