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
Blog

Khám Phá Các Thành Phần Cơ Bản Kiến Tạo Nên Ứng Dụng NestJS Mạnh Mẽ

by Nguyen Pham
02/09/2025
0

Chào mừng các bạn đến với thế giới của NestJS – một framework Node.js tiến bộ, linh hoạt và cực...

Read more

`useState` trong React: Nền Tảng Quản Lý Trạng Thái cho Component Hàm

02/09/2025
Hướng Dẫn Cài Đặt và Chạy n8n Với Docker – Tự Động Hóa Công Việc Dễ Dàng

Hướng Dẫn Cài Đặt và Chạy n8n Với Docker – Tự Động Hóa Công Việc Dễ Dàng

02/09/2025
GPT OSS: Tương Lai Mã Nguồn Mở Cho AI Ngôn Ngữ

GPT OSS: Tương Lai Mã Nguồn Mở Cho AI Ngôn Ngữ

17/08/2025

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

26/04/2025

@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