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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

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

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

โดย mindphp » 15/02/2017 7:07 pm

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

หรือ อีกวิธีคือ return ค่า เป็น dict
แล้ว ตอนเอาไปใช้ค่อย loop ดึงแต่ละค่าไปต่อ string เอาอีกที

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

โดย Dive Demo » 15/02/2017 7:01 pm

ตอนนี้ผมลองแก้ใหม่แล้วครับลอง 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 846 times

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

โดย mindphp » 15/02/2017 12:45 pm

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

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

แล้วเอาค่าที่ได้ของฟังก์ชั่น ไปต่อ string ตัวแปล html
ให้ทำทั้งหมด ในฟังก์ชั่น create_app

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

โดย Dive Demo » 15/02/2017 12:19 pm

สวัสดีครับผมติดปัญหาจากโจทย์ที่ผมได้รับด้วยภาษา 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)

ข้างบน