Làm cách nào để tạo thư mục con trong python?

Đôi khi chúng ta cần di chuyển toàn bộ thư mục hoặc có thể có một danh sách các thư mục như vậy nói A cùng với nội dung con, tệp và thư mục con của nó sang một thư mục đích khác B. Mặc dù điều này có thể được thực hiện thủ công bằng cách 'cắt' và 'dán' nhưng nếu có hàng trăm hoặc hàng nghìn thư mục bạn muốn di chuyển thì sao, chưa kể đến lỗi của con người. Hãy xem cách thực hiện điều này một cách dễ dàng trong python với vài dòng mã bằng cách sử dụng mô-đun Shutil

mô-đun đóng cửa

Shutil cung cấp một số chức năng cấp cao giúp tự động hóa quá trình sao chép, di chuyển hoặc xóa tệp hoặc thư mục bất kể nền tảng được sử dụng. Nó đi kèm với các mô-đun tiện ích tiêu chuẩn của Python, vì vậy không cần cài đặt riêng

Nó có cửa chớp. move[] phương thức di chuyển đệ quy một tệp hoặc thư mục [nguồn] cùng với nội dung phụ của nó đến một vị trí khác [đích] và trả về đích. Nếu thư mục đích đã tồn tại thì nguồn sẽ được di chuyển vào bên trong thư mục đó, nếu không, một thư mục mới sẽ được tạo trước khi di chuyển. Nếu đích đã tồn tại nhưng không phải là một thư mục thì nó có thể bị ghi đè hoặc gây ra lỗi tùy thuộc vào hệ điều hành. đổi tên [] thông số kỹ thuật

cú pháp. đóng cửa. di chuyển [nguồn, đích, copy_function = copy2]

Giả sử cấu trúc của thư mục trông như thế này -

 

trong đó 'dest' là thư mục đích của chúng tôi

Làm cách nào để di chuyển danh sách các thư mục có thư mục con bằng Python?

ví dụ 1. Dựa trên danh sách thư mục

Ở đây chúng tôi đã tạo một danh sách các thư mục mà chúng tôi muốn di chuyển. Để đơn giản, tất cả các thư mục nằm trong cùng một thư mục, bạn cũng có thể di chuyển từ các thư mục cơ sở khác nhau.  

Python3




# import shutil module

import shutil

 

# import os module

import os

 

# base path

base_path= # import shutil module0

 

# import shutil module1

# import shutil module2__= # import shutil module4# import shutil module5# import shutil module6# import shutil module7# import shutil module6# import shutil module9# import shutil module6import1import2

 

import3

import4= import6import7import8

 

import9shutil0shutil1import8

import9shutil4

 

shutil5

shutil6 shutil7____38 shutil9

 

# import os module0# import os module1

# import os module0# import os module3

# import os module0# import os module5____9 # import os module7

 

# import os module0# import os module9

# import os module0import1 import2

 

import3____24

import3____26

 

import9shutil0____29import8

import9shutil4

đầu ra

Trước khi di chuyển thư mục

[‘dest’, ‘test1’, ‘test2’, ‘test3’, ‘test4’, ‘test5’, ‘web_tools_express’, ‘web_tools_html’, ‘web_tools_node’, ‘web_tools_react’]

Sau khi di chuyển thư mục

[‘dest’, ‘test1’, ‘test3’, ‘web_tools_express’, ‘web_tools_html’, ‘web_tools_node’, ‘web_tools_react’]

Hãy kiểm tra thư mục đích, đó là

 

Như bạn có thể thấy, chúng tôi đã chuyển toàn bộ thư mục và nội dung của chúng sang thư mục đích [đường dẫn tuyệt đối trỏ đến thư mục 'dest']

ví dụ 2. Dựa trên mẫu

Giả sử chúng tôi muốn di chuyển các thư mục theo một mẫu tên cụ thể đến đích của chúng tôi. Hãy di chuyển tất cả các thư mục có tên bắt đầu bằng 'web'. Bạn có thể sử dụng bất kỳ mẫu nào theo nhu cầu của bạn. Đây sẽ là trường hợp sử dụng thực tế hơn khi chúng tôi có hàng trăm thư mục

Python3




# import shutil module

import shutil

 

# import os module

import os

 

# base path

base_path= # import shutil module0

 

# base path3

# base path4= # base path6

 

import3

import4= import6import7import8

 

import9shutil0shutil1import8

import9shutil4

 

shutil6 shutil7____38 =2

 

# import os module0____94

# import os module0=6

# import os module0import1 =9____100# import shutil module01

 

import3____103

import3______45____9 # import os module7

 

import3____24

import3____26

 

import9shutil0____29import8

import9shutil4

đầu ra

Trước khi di chuyển thư mục

[‘dest’, ‘test1’, ‘test3’, ‘web_tools_express’, ‘web_tools_html’, ‘web_tools_node’, ‘web_tools_react’]

Sau khi di chuyển thư mục

[‘dest’, ‘test1’, ‘test3’]

Hãy kiểm tra thư mục đích, đó là

 

ví dụ 3. Dựa vào thời gian tạo

Python3




# import shutil module

import shutil

# import os module

import os

# base path

base_path= # import shutil module0

# base path3

# base path4= # base path6

import3

import4= import6import7import8

import1 # import shutil module39 # import shutil module40

# import os module0# import shutil module42

 

import9shutil0shutil1import8

import9# import shutil module48shutil6 # import shutil module50shutil8 # base path6import1 # import shutil module54

shutil6 shutil7____38 =2

# import os module0# import shutil module60

# import os module0# import shutil module62____9 # import os module7

# import os module0import1 # import shutil module67

import3______169____9 # import shutil module71

import3______173= # import shutil module75

import3import1 # import shutil module78# import shutil module79# import shutil module80# import shutil module81 # import shutil module82# import shutil module83# import shutil module84 # import shutil module85# import shutil module86# import shutil module87

# import shutil module88# import shutil module03

# import shutil module88# import os module5= # import os module7

# import shutil module88import4

# import shutil module88import6

import9shutil0____29import8

import9# import shutil module48shutil6 # import shutil module50shutil8 # base path6import1 # import shutil module54

đầu ra

Before moving directories:
['btechmarksheet_files', 'chromedriver_win32 [1]', 'dest', 'How to make a fully transparent window with PyGame _-Write_files', 'gfg', 'tuhin • gfg photos and videos_files']
After moving directories:
['dest']

Giải trình

Ở đây chúng tôi đã tính toán sự khác biệt giữa thời gian tạo thư mục và ngày hôm nay và nếu sự khác biệt lớn hơn 7 ngày thì chúng tôi có thể di chuyển thư mục đó đến đích nếu không thì bỏ qua điều đó

Chủ Đề