2k1
  • Home
  • Programming
  • System
  • Design
  • Applications
  • Tech
No Result
View All Result
  • Login
2k1
  • Home
  • Programming
  • System
  • Design
  • Applications
  • Tech
No Result
View All Result
2k1
No Result
View All Result

Cách sắp xếp các file trong một folder gọn gàng sử dụng python

Nguyen Pham by Nguyen Pham
01/08/2020
in Programming, Python, Uncategorized
Reading Time: 2 mins read
A A
0

Thư viện sử dụng

  • os
  • shutil

Giải thích

os.listdir() // liệt kê các folder va file ở nơi file python tồn tại

os.getcwd() // lấy đường dẫn file hiện tại

os.mkdir() //tạo folder mới

os.path.join(path,*arg) //nối đường dẫn

os.path.exists(path) // Kiểm tra folder tồn tại hay không đúng trả về True

os.path.isfile(path) // Kiểm tra có phải là một file hay folder

shutil.move(old path,new path) //di chuyển file

import os
import shutil
#Loc cac tep
list=os.listdir()
Compressed=('zip','rar')
Documents=()
Picture=('jpg','PNG')
Programs=('exe')
Video=('mp4')
Music=('mp3','flac')
#cac folder
listdir=('Picture','Documents','Programs','Video','Music','Compressed')

for i in listdir:
    if(os.path.exists(os.path.join(os.getcwd(), i))):
        pass
    else:
        os.mkdir(os.path.join(os.getcwd(), i))
#loc file
for i in list:
    if (os.path.isfile(os.path.join(os.getcwd(), i))):
        a=i.split('.')
        if(a[len(a)-1] in Picture):
            try:
                shutil.move(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), 'Picture',i))
            finally:
                pass
        if (a[len(a) - 1] in Documents):
            try:
                shutil.move(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), 'Documents', i))
            finally:
                pass
        if (a[len(a) - 1] in Programs):
            try:
                shutil.move(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), 'Programs', i))
            finally:
                pass
        if (a[len(a) - 1] in Video):
            try:
                shutil.move(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), 'Video', i))
            finally:
                pass
        if (a[len(a) - 1] in Compressed):
            try:
                shutil.move(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), 'Compressed', i))
            finally:
                pass
        if (a[len(a) - 1] in Music):
            try:
                shutil.move(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), 'music', i))
            finally:
                pass
Tags: xăp xếp file
Previous Post

Luminar 3 – Luminar 2018 & Aurora HDR 2018

Next Post

Warzone 2100 đang miễn phí trên Steam

Related Posts

Xây dựng todo app với smartcontract
Go

Giới thiệu về đa luồng trong ngôn ngữ lập trình go có code minh họa.

by Nguyen Pham
29/06/2023
Programming

Lập trình rust cơ bản – vòng lặp

by Nguyen Pham
22/12/2022
Programming

Lập trình python cơ bản – Hello world

by Nguyen Pham
16/11/2022
Programming

Lập trình go cơ bản – Hello world

by Nguyen Pham
04/12/2022
Programming

Lập trình rust cơ bản – Biến

by Nguyen Pham
16/11/2022
Programming

Lập trình rust cơ bản – hello world

by Nguyen Pham
14/11/2022
Load More
Next Post

Warzone 2100 đang miễn phí trên Steam

Please login to join discussion
Stock

Phân tích mã cổ phiếu VCB

by Nguyen Pham
26/04/2025
0

Phân tích mã cổ phiếu VCB (Ngân hàng TMCP Ngoại thương Việt Nam - Vietcombank) 1. Tổng quan về Vietcombank...

Read more

Facebook, Instagram bất ngờ sập trên diện rộng, liên tục đăng xuất người dùng!

05/03/2024
Xây dựng todo app với smartcontract

Web3 là gì?

30/06/2023
Xây dựng todo app với smartcontract

Giới thiệu về đa luồng trong ngôn ngữ lập trình go có code minh họa.

29/06/2023

Chạy ứng dụng react native đầu tiên của bạn

29/06/2023

@2021 2k1.org [email protected]

No Result
View All Result
  • Home
  • Review
  • Applications
  • Computers
  • Gaming
  • Microsoft

© 2021 NData

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In