อยากทราบวิธีให้แสดงชื่อไฟล์ทั้งหมดที่อยู่ในโฟลดเดอร์แสดงบนเว็บ ด้วย Python

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

Dive Demo
PHP Sr. Member
PHP Sr. Member
โพสต์: 84
ลงทะเบียนเมื่อ: 01/02/2017 11:10 am

อยากทราบวิธีให้แสดงชื่อไฟล์ทั้งหมดที่อยู่ในโฟลดเดอร์แสดงบนเว็บ ด้วย Python

โพสต์ที่ยังไม่ได้อ่าน โดย Dive Demo »

สวัสดีครับผมติดปัญหาจากโจทย์ที่ผมได้รับด้วยภาษา Python ครับ โดยโจทย์กำหนดว่า 1.เขียนคำสั่งเปลี่ยนนามสกุลไฟล์ทั้งหมดในโฟลดเดอร์จากสกุลไฟล์เดิมให้เป็น png ด้วย ImgaeMagic 2.หลังจากเปลี่ยนสกุลไฟล์เสร็จแล้วเรียบให้แสดงผลบนเว็บเป็นโดยเขียนคำสั่งเรียก ชื่อไฟล์ที่เปลี่ยนมา ให้แสดงผล เช่น test1.png test2.png

จากการพัฒนาตอนนี้ผมใช้ super class โดยให้ทำงานที่ฟังก์ชั่น __init__ ก่อนหลังจากให้ให้เข้ามาทำงานต่อฟังก์ชั่นที่ 2 โดยเรียกแสดงผลชื่อไฟล์

โค้ด: เลือกทั้งหมด

# -*- coding: utf-8 -*-
import time
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
import os
import PythonMagick as Magick
class parent(object):
    def __init__(self, up):
        print childA()
    def test(file):
        img = Magick.Image('../img_magic/img_m/' + file)
        img.quality(100)
        img.magick('PNG')
        img.write('../img_magic/img_m/' + file + '.png')
    f = os.listdir("../img_magic/img_m")
    for file in f:
        print file
        test(file)
class childA(parent):
    def __init__(self):
        super (childA, self).test()
childA()

import os
fee = os.listdir("../img_magic/img_m")
for fill in fee:
      print fee
html = """
 """

def wsgi_app(self, environ, start_response):
        request = Request(environ)
        response = self.dispatch_request(request)
        return response(environ, start_response)
    
def create_app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [parent + html + 'This time ---->  ' + time.ctime()]

run_simple('127.0.0.1', 5012, create_app, use_debugger=True, use_reloader=True)
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41125
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: อยากทราบวิธีให้แสดงชื่อไฟล์ทั้งหมดที่อยู่ในโฟลดเดอร์แสดงบนเว็บ ด้วย Python

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ในโค้ด มี import os ซ้ำกับ

ลอง ดูเกี่ยวกับ ฟังก์ชั่นที่เกี่ยวกับ return ค่า
https://www.youtube.com/watch?v=T37IyoSgeP0

แล้วเอาค่าที่ได้ของฟังก์ชั่น ไปต่อ string ตัวแปล html
ให้ทำทั้งหมด ในฟังก์ชั่น create_app
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
Dive Demo
PHP Sr. Member
PHP Sr. Member
โพสต์: 84
ลงทะเบียนเมื่อ: 01/02/2017 11:10 am

Re: อยากทราบวิธีให้แสดงชื่อไฟล์ทั้งหมดที่อยู่ในโฟลดเดอร์แสดงบนเว็บ ด้วย Python

โพสต์ที่ยังไม่ได้อ่าน โดย Dive Demo »

ตอนนี้ผมลองแก้ใหม่แล้วครับลอง return ค่าดูแต่ผลลัพท์คือชื่อไฟล์ออกมาแค่ชื่อเดียวอ่ะครับ

โค้ด: เลือกทั้งหมด

# -*- coding: utf-8 -*-
import time
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
import os
import PythonMagick as Magick
def test(file):
    t_file = file
    img = Magick.Image('../img_magic/img_m/' + file)
    img.quality(100)
    img.magick('PNG')
    img.write('../img_magic/img_m/' + file + '.png')
    return t_file
f = os.listdir("../img_magic/img_m")
for file in f:
    file_up = file
    print file
    test(file)
fee = os.listdir("../img_magic/img_m")
for file_ee in fee:
    file_up = file_ee
    print file_ee
html = """
 """

def wsgi_app(self, environ, start_response):
        request = Request(environ)
        response = self.dispatch_request(request)
        return response(environ, start_response)
    
def create_app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [file_up + html + 'This time ---->  ' + time.ctime()]

run_simple('127.0.0.1', 5012, create_app, use_debugger=True, use_reloader=True)
Output
Selection_136.png
Selection_136.png (4.67 KiB) Viewed 838 times
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41125
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: อยากทราบวิธีให้แสดงชื่อไฟล์ทั้งหมดที่อยู่ในโฟลดเดอร์แสดงบนเว็บ ด้วย Python

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ถ้า อยู่ใน loop ให้ลองต่อ string ใน loop
แล้วค่อย return ค่าออกมา

หรือ อีกวิธีคือ return ค่า เป็น dict
แล้ว ตอนเอาไปใช้ค่อย loop ดึงแต่ละค่าไปต่อ string เอาอีกที
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: witsarutt000 และบุคลทั่วไป 39