วิธีการแบ่งนามสกุลไฟล์เพื่อนำมาตรวจสอบ โดยใช้ os.path.splitext() ในภาษา python
ใครที่กำลังหาวิธีตรวจสอบนามสกุลไฟล์หรือจะแบ่งนามสกุลไฟล์นะครับ ผมมีวิธีใช้มาแชร์นะครับ ก่อนอื่นเรามารู้จัก ตัว
library os ในภาษา
Python นะครับ library os คือ จะใช้สำหรับเซื่อมต่อหรือจัดการสิ่งที่เกี่ยวกับระบบปฏิบัติการ ซึ่งในการเรียกใช้งานนั้นต้องทำการ
import เข้ามาใช้ครับ รูปแบบการใช้ตัว os.path.splitext() ก็จะมีดังนี้
ทำการ import module os
ในตัวอย่างผมจะยกตัวอย่างในการ upload file เข้ามาและมาตรวงสอบนะครับ
โค้ดคำสั่งหน้า HTML
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="static/css/datatables.min.css">
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/datatables.min.js"></script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<br>
<br>
<div class="container-fluid">
<div class="row">
<div class="col-md-3" ></div>
<div class="col-md-6" >
<div class="card">
<div class="card-header" align="center">Upload File</div>
<div class="card-body">
{%with messages = get_flashed_messages()%}
{%if messages %}
{% for message in messages%}
<div class="alert alert-warning alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Warning! </strong>{{message}}
</div>
{%endfor%}
{% endif %}
{%endwith%}
<form action="/uploaded" method="POST" enctype="multipart/form-data">
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="customFile" name="fileupload">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">upload</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-3" ></div>
</div>
</div>
<script>
$(".custom-file-input").on("change", function() {
var fileName = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
</script>
<br>
</body>
</html>
โค้ดคำสั่ง Python
โค้ด: เลือกทั้งหมด
from flask import Flask, render_template,request,flash
import os.path
UPLOAD_FOLDER = '/home/com001/PycharmProjects/test2/uploadfile'
app=Flask(__name__)
app.secret_key = 'ff938fl//jvl;a93kff9qp@Rifomc'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def upload():
return render_template('uploadfile.html')
@app.route('/uploaded', methods=['POST'])
def uploaded():
f = request.files['fileupload']
fileupload = os.path.splitext(f.filename)
if fileupload[1] != '.html':
flash('You incorrect upload file. Try Again')
return render_template('uploadfile.html')
f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename))
return 'Upload File Success'
if __name__=='__main__':
app.run()
จากตัวอย่างผมกำหนด upload แค่ไฟล์ HTML
ผลที่ได้
- Selection_016.png (6.89 KiB) Viewed 1778 times
upload file pgadmin.log เข้ามา
- Selection_017.png (12.75 KiB) Viewed 1778 times
ปรากฎว่า upload ไม่ได้แต่ลอง upload file html
- Selection_018.png (12.55 KiB) Viewed 1778 times
upload file test.html เข้ามา
- Selection_019.png (6.34 KiB) Viewed 1778 times
อ้างอิง
https://www.stackoverflow.com/questions/5899497/checking-file-extension
https://www.docs.python.org/2/library/os.path.html
https://www.geeksforgeeks.org/python-os-path-splitext-method/
[b]วิธีการแบ่งนามสกุลไฟล์เพื่อนำมาตรวจสอบ โดยใช้ os.path.splitext() ในภาษา python[/b]
ใครที่กำลังหาวิธีตรวจสอบนามสกุลไฟล์หรือจะแบ่งนามสกุลไฟล์นะครับ ผมมีวิธีใช้มาแชร์นะครับ ก่อนอื่นเรามารู้จัก ตัว [url=https://www.mindphp.com/forums/viewtopic.php?p=156073#p156073]library os[/url] ในภาษา [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Python[/url] นะครับ library os คือ จะใช้สำหรับเซื่อมต่อหรือจัดการสิ่งที่เกี่ยวกับระบบปฏิบัติการ ซึ่งในการเรียกใช้งานนั้นต้องทำการ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python/2495-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89-from-import-modules.html]import[/url] เข้ามาใช้ครับ รูปแบบการใช้ตัว os.path.splitext() ก็จะมีดังนี้
ทำการ import module os
[code]
import os
[/code]
ในตัวอย่างผมจะยกตัวอย่างในการ upload file เข้ามาและมาตรวงสอบนะครับ
โค้ดคำสั่งหน้า HTML
[code]
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="static/css/datatables.min.css">
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/datatables.min.js"></script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<br>
<br>
<div class="container-fluid">
<div class="row">
<div class="col-md-3" ></div>
<div class="col-md-6" >
<div class="card">
<div class="card-header" align="center">Upload File</div>
<div class="card-body">
{%with messages = get_flashed_messages()%}
{%if messages %}
{% for message in messages%}
<div class="alert alert-warning alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Warning! </strong>{{message}}
</div>
{%endfor%}
{% endif %}
{%endwith%}
<form action="/uploaded" method="POST" enctype="multipart/form-data">
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="customFile" name="fileupload">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">upload</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-3" ></div>
</div>
</div>
<script>
$(".custom-file-input").on("change", function() {
var fileName = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
</script>
<br>
</body>
</html>
[/code]
โค้ดคำสั่ง Python
[code]
from flask import Flask, render_template,request,flash
import os.path
UPLOAD_FOLDER = '/home/com001/PycharmProjects/test2/uploadfile'
app=Flask(__name__)
app.secret_key = 'ff938fl//jvl;a93kff9qp@Rifomc'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def upload():
return render_template('uploadfile.html')
@app.route('/uploaded', methods=['POST'])
def uploaded():
f = request.files['fileupload']
fileupload = os.path.splitext(f.filename)
if fileupload[1] != '.html':
flash('You incorrect upload file. Try Again')
return render_template('uploadfile.html')
f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename))
return 'Upload File Success'
if __name__=='__main__':
app.run()
[/code]
จากตัวอย่างผมกำหนด upload แค่ไฟล์ HTML
ผลที่ได้
[attachment=3]Selection_016.png[/attachment]
upload file pgadmin.log เข้ามา
[attachment=2]Selection_017.png[/attachment]
ปรากฎว่า upload ไม่ได้แต่ลอง upload file html
[attachment=1]Selection_018.png[/attachment]
upload file test.html เข้ามา
[attachment=0]Selection_019.png[/attachment]
อ้างอิง
https://www.stackoverflow.com/questions/5899497/checking-file-extension
https://www.docs.python.org/2/library/os.path.html
https://www.geeksforgeeks.org/python-os-path-splitext-method/