Hướng dẫn os remove folder python - os xóa thư mục python

Làm cách nào để xóa một tệp hoặc thư mục trong Python?

Đối với Python 3, để xóa từng tệp và thư mục, hãy sử dụng các phương thức đối tượng

Show
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
2 và
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
3
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
4 tương ứng:

Nội dung chính ShowShow

  • Làm cách nào để xóa một tệp hoặc thư mục trong Python?
  • Trình diễn
  • Xóa tệp/Dir bằng phương thức Os.Remove ()
  • Cú pháp của Os.Remove ()
  • Xóa tệp/Dir bằng phương thức OS.RMDIR ()
  • Cú pháp của OS.RMDIR ()
  • Phương thức OS.RMDIR () trong Python được sử dụng để xóa hoặc xóa một thư mục trống. Oserror sẽ được nâng lên nếu đường dẫn được chỉ định không phải là một thư mục trống.
  • Cú pháp: os.rmdir (đường dẫn, *, dir_fd = none) & nbsp;
  • Loại trả về: Phương thức này không trả về bất kỳ giá trị nào.
  • Xóa tệp/Dir
  • Cú pháp của pathlib.path
  • Làm thế nào để bạn xóa một tệp hoặc thư mục trong Python?
  • Làm cách nào để xóa một thư mục đầy đủ trong Python?
  • Làm cách nào để xóa một thư mục?
  • Làm thế nào để bạn xóa một tệp trong Python?

from pathlib import Path
dir_path = Path.home() / 'directory' 
file_path = dir_path / 'file'

file_path.unlink() # remove file

dir_path.rmdir()   # remove directory

Lưu ý rằng bạn cũng có thể sử dụng các đường dẫn tương đối với các đối tượng

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
4 và bạn có thể kiểm tra thư mục làm việc hiện tại của mình với
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
6.

Để xóa các tệp và thư mục riêng lẻ trong Python 2, hãy xem phần được dán nhãn bên dưới.

Để xóa một thư mục có nội dung, hãy sử dụng

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
7 và lưu ý rằng điều này có sẵn trong Python 2 và 3:
from shutil import rmtree

rmtree(dir_path)

Trình diễn

Xóa tệp/Dir bằng phương thức Os.Remove ()

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
4.

Cú pháp của Os.Remove ()

Xóa tệp/Dir bằng phương thức OS.RMDIR ()

Cú pháp của OS.RMDIR ()

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
0

Phương thức OS.RMDIR () trong Python được sử dụng để xóa hoặc xóa một thư mục trống. Oserror sẽ được nâng lên nếu đường dẫn được chỉ định không phải là một thư mục trống.

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
4

Cú pháp: os.rmdir (đường dẫn, *, dir_fd = none) & nbsp;

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
5

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào.

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
6

Xóa tệp/Dir

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
7

Cú pháp của pathlib.path

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
7

Làm thế nào để bạn xóa một tệp hoặc thư mục trong Python?

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
9

Làm cách nào để xóa một thư mục đầy đủ trong Python?

Làm cách nào để xóa một thư mục?

Làm thế nào để bạn xóa một tệp trong Python?

from shutil import rmtree

rmtree(dir_path)
0

Lưu ý rằng bạn cũng có thể sử dụng các đường dẫn tương đối với các đối tượng

from shutil import rmtree

rmtree(dir_path)
1

from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 4 và bạn có thể kiểm tra thư mục làm việc hiện tại của mình với from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 6.

Để xóa các tệp và thư mục riêng lẻ trong Python 2, hãy xem phần được dán nhãn bên dưới.

from shutil import rmtree

rmtree(dir_path)
2

Để xóa một thư mục có nội dung, hãy sử dụng

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
04
from shutil import rmtree

rmtree(dir_path)
3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
7 và lưu ý rằng điều này có sẵn trong Python 2 và 3:
from shutil import rmtree

rmtree(dir_path)

Mới trong Python 3.4 là đối tượng

Hãy sử dụng một để tạo một thư mục và tệp để chứng minh việc sử dụng. Lưu ý rằng chúng tôi sử dụng

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
9 để tham gia các phần của đường dẫn, điều này hoạt động xung quanh các vấn đề giữa các hệ điều hành và các vấn đề từ việc sử dụng dấu gạch chéo ngược trên Windows (nơi bạn cần tăng gấp đôi các dấu gạch chéo ngược của mình như
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
00 hoặc sử dụng các chuỗi thô, như
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
01) :
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()

và bây giờ:

from shutil import rmtree

rmtree(dir_path)
6

Bây giờ chúng ta hãy xóa chúng. Đầu tiên là tệp:

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
08:
from shutil import rmtree

rmtree(dir_path)
7

Chúng ta có thể sử dụng Globbing để xóa nhiều tệp - trước tiên hãy tạo một vài tệp cho việc này:

Sau đó, chỉ lặp lại mô hình toàn cầu:

Bây giờ, chứng minh loại bỏ thư mục:

Điều gì sẽ xảy ra nếu chúng ta muốn xóa một thư mục và mọi thứ trong đó? Đối với trường hợp sử dụng này, hãy sử dụng

  • Hãy tạo lại thư mục và tệp của chúng tôi:
  • Và lưu ý rằng
  • from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    3 không thành công trừ khi nó trống, đó là lý do tại sao RMtree rất thuận tiện:
    from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    1
  • Bây giờ, nhập RMtree và chuyển thư mục vào tiêu tốn:

Xóa tệp/Dir bằng phương thức Os.Remove ()

Cú pháp của Os.Remove () in Python provides functions for interacting with the operating system. All functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system. 

Xóa tệp/Dir bằng phương thức OS.RMDIR () is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.

Cú pháp của Os.Remove ()

Xóa tệp/Dir bằng phương thức OS.RMDIR () os.remove(path, *, dir_fd = None) 

Tham số: Đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn tệp. Một đối tượng giống như đường dẫn là một đối tượng chuỗi hoặc byte đại diện cho một đường dẫn. & Nbsp;path: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.  path: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path. 

  • DIR_FD (Tùy chọn): Một mô tả tệp đề cập đến một thư mục. Giá trị mặc định của tham số này là không có. Nếu đường dẫn được chỉ định là tuyệt đối thì dir_fd bị bỏ qua. & Nbsp;A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored. A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored. 

Lưu ý: Danh sách tham số ‘*trong danh sách tham số chỉ ra rằng tất cả các tham số sau (ở đây trong trường hợp của chúng tôi‘ DIR_FD,) là các tham số chỉ dành cho từ khóa và chúng có thể được cung cấp bằng tên của chúng, không làm tham số vị trí. & NBSP; The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter.  The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter. 

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào.This method does not return any value.This method does not return any value.

Ví dụ: Xóa một thư mục trống bằng rmdir ()

Trong ví dụ này, chúng tôi sẽ xóa một thư mục trống, chúng tôi chỉ cần chỉ định tên thư mục nếu nó nằm trong thư mục gốc

Hướng dẫn os remove folder python - os xóa thư mục python

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
04

Python3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
08
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
10
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
11
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
14
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
17
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
19
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
53

Output:   

Làm thế nào để bạn xóa một tệp hoặc thư mục trong Python?

Xóa một tập tin hoặc thư mục trong Python.

Python3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
58
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
45
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
61
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
50
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
5 3
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
70
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52

Output:

from shutil import rmtree

rmtree(dir_path)
8

Ví dụ 3: Kiểm tra xem tệp có tồn tại trước khi xóa khôngCheck if File Exists Before Deleting Check if File Exists Before Deleting

Xử lý lỗi trong khi sử dụng phương thức os.Remove ().

Python3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
78
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
79
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
90
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
53
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
96
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
98
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
99
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
10
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
13
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
17
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
5 2

Output:

from shutil import rmtree

rmtree(dir_path)
9

Lưu ý: Để biết thêm về Os.Remove () Bấm vào đây.To know more about os.remove() click here.To know more about os.remove() click here.

Xóa tệp/Dir bằng phương thức OS.RMDIR ()

Phương thức OS.RMDIR () trong Python được sử dụng để xóa hoặc xóa một thư mục trống. Oserror sẽ được nâng lên nếu đường dẫn được chỉ định không phải là một thư mục trống.OSError will be raised if the specified path is not an empty directory.OSError will be raised if the specified path is not an empty directory.

Cú pháp của OS.RMDIR ()

Cú pháp: os.rmdir (đường dẫn, *, dir_fd = none) & nbsp;os.rmdir(path, *, dir_fd = None) os.rmdir(path, *, dir_fd = None) 

Parameter:   

  • Đường dẫn: Một đối tượng giống như đường dẫn đại diện cho một đường dẫn tệp. Một đối tượng giống như đường dẫn là một đối tượng chuỗi hoặc byte đại diện cho một đường dẫn. & Nbsp; A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.  A path-like object representing a file path. A path-like object is either a string or bytes object representing a path. 
  • DIR_FD (Tùy chọn): Một mô tả tệp đề cập đến một thư mục. Giá trị mặc định của tham số này là không có. Nếu đường dẫn được chỉ định là tuyệt đối thì dir_fd bị bỏ qua. & Nbsp; A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored.  A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored. 

Lưu ý: Danh sách tham số ‘*trong danh sách tham số chỉ ra rằng tất cả các tham số sau (ở đây trong trường hợp của chúng tôi‘ DIR_FD,) là các tham số chỉ dành cho từ khóa và chúng có thể được cung cấp bằng tên của chúng, không làm tham số vị trí. & NBSP; The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter.  The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter. 

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào.This method does not return any value. This method does not return any value.

Ví dụ: Xóa một thư mục trống bằng rmdir () Delete all directories from a Directory

Trong ví dụ này, chúng tôi sẽ xóa một thư mục trống, chúng tôi chỉ cần chỉ định tên thư mục nếu nó nằm trong thư mục gốc

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
04

Python3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
08
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
10
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
11
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
14
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
17
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
19
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
53

Output:   

Làm thế nào để bạn xóa một tệp hoặc thư mục trong Python?

Xóa một tập tin hoặc thư mục trong Python.

Python3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from shutil import rmtree

rmtree(dir_path)
01
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
15
from shutil import rmtree

rmtree(dir_path)
04
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
06
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
09
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
79
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
90
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from shutil import rmtree

rmtree(dir_path)
10

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
99
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
10
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
13

Lưu ý: Để biết thêm về Os.Remove () Bấm vào đây.

Output:

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
0

Xóa tệp/Dir bằng phương thức OS.RMDIR () To know more about os.rmdir() click here. To know more about os.rmdir() click here.

Phương thức OS.RMDIR () trong Python được sử dụng để xóa hoặc xóa một thư mục trống. Oserror sẽ được nâng lên nếu đường dẫn được chỉ định không phải là một thư mục trống.

Cú pháp của OS.RMDIR ()

Cú pháp: os.rmdir (đường dẫn, *, dir_fd = none) & nbsp;

Đường dẫn: Một đối tượng giống như đường dẫn đại diện cho một đường dẫn tệp. Một đối tượng giống như đường dẫn là một đối tượng chuỗi hoặc byte đại diện cho một đường dẫn. & Nbsp; shutil.rmtree(path, ignore_errors=False, onerror=None)  shutil.rmtree(path, ignore_errors=False, onerror=None) 

Parameters:   

  • Đường dẫn: Một đối tượng giống như đường dẫn đại diện cho một đường dẫn tệp. Một đối tượng giống như đường dẫn là một đối tượng chuỗi hoặc byte đại diện cho một đường dẫn. & Nbsp;A path-like object representing a file path. A path-like object is either a string or bytes object representing a path. A path-like object representing a file path. A path-like object is either a string or bytes object representing a path. 
  • DIR_FD (Tùy chọn): Một mô tả tệp đề cập đến một thư mục. Giá trị mặc định của tham số này là không có. Nếu đường dẫn được chỉ định là tuyệt đối thì dir_fd bị bỏ qua. & Nbsp; If ignore_errors is true, errors resulting from failed removals will be ignored.  If ignore_errors is true, errors resulting from failed removals will be ignored. 
  • Lưu ý: Danh sách tham số ‘*trong danh sách tham số chỉ ra rằng tất cả các tham số sau (ở đây trong trường hợp của chúng tôi‘ DIR_FD,) là các tham số chỉ dành cho từ khóa và chúng có thể được cung cấp bằng tên của chúng, không làm tham số vị trí. & NBSP; If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror. If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào.

Ví dụ 1: Xóa tất cả các thư mục khỏi thư mục  

Giả sử các thư mục là - & nbsp;

Chúng tôi muốn loại bỏ các chuyên viên viên thư mục. Dưới đây là việc thực hiện. & NBSP;# Parent directory: # Parent directory: 

from shutil import rmtree

rmtree(dir_path)
01
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
03# Directory inside parent directory: # Directory inside parent directory: 

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
3604
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
06 04
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
06
 

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
398
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
098
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
09

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

Python3

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from shutil import rmtree

rmtree(dir_path)
01
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
15
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
5029
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from shutil import rmtree

rmtree(dir_path)
31
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
50
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52
from shutil import rmtree

rmtree(dir_path)
58

Output:   

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
598
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
70
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
528
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
70
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52

Ví dụ 3: Kiểm tra xem tệp có tồn tại trước khi xóa không

Python3

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from shutil import rmtree

rmtree(dir_path)
01
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
15
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
5029
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from shutil import rmtree

rmtree(dir_path)
31
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
50
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52
from shutil import rmtree

rmtree(dir_path)
58

Output:

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
598
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
70
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52

Ví dụ 3: Kiểm tra xem tệp có tồn tại trước khi xóa không

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
41
from shutil import rmtree

rmtree(dir_path)
01
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
15
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
91
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from shutil import rmtree

rmtree(dir_path)
29
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from shutil import rmtree

rmtree(dir_path)
31
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
50
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
56
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52
from shutil import rmtree

rmtree(dir_path)
74
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from shutil import rmtree

rmtree(dir_path)
76
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
52

  • from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    68
    from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    69
    from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    70
    from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    71
    from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    56
    from pathlib import Path
    
    # .home() is new in 3.5, otherwise use os.path.expanduser('~')
    directory_path = Path.home() / 'directory'
    directory_path.mkdir()
    
    file_path = directory_path / 'file'
    file_path.touch()
    
    52
    function which raised the exception.
  • Trong OnError, hàm nên được truyền phải chứa ba tham số. path name passed which raised the exception while removal
  • Chức năng - Chức năng nâng cao ngoại lệ.function which raised the exception. exception info raised by sys.exc_info()

Đường dẫn - tên đường dẫn được truyền đi trong khi loại bỏ path name passed which raised the exception while removal

Python3

ExcInfo - Thông tin ngoại lệ được nêu bởi sys.exc_info () exception info raised by sys.exc_info()

Output:

Dưới đây là triển khai & NBSP;

from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 40 from shutil import rmtree rmtree(dir_path) 44from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 40 from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 41from shutil import rmtree rmtree(dir_path) 82 from shutil import rmtree rmtree(dir_path) 83from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 91from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 68from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 69from shutil import rmtree rmtree(dir_path) 87from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 52from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 91from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 68from shutil import rmtree rmtree(dir_path) 91from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 45from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 43 from shutil import rmtree rmtree(dir_path) 49from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 56 from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 43 from shutil import rmtree rmtree(dir_path) 52from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 48from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 43 from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 50from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 56from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 52from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 03from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 43from pathlib import Path # .home() is new in 3.5, otherwise use os.path.expanduser('~') directory_path = Path.home() / 'directory' directory_path.mkdir() file_path = directory_path / 'file' file_path.touch() 05

Bên trong Handler (, FilenotFounderror (2, System Hệ thống không thể tìm thấy đường dẫn được chỉ định),) bên trong Handler (, FilenotFounderror (2, System Hệ thống không thể tìm thấy tệp được chỉ định),)

Xóa tệp/Dir

Một thư mục trống cũng có thể được xóa hoặc xóa bằng phương thức Module PathLib Module RMDIR (). Đầu tiên, chúng tôi phải & nbsp; đặt đường dẫn cho thư mục, và sau đó chúng tôi & nbsp; gọi phương thức rmdir () trên đường dẫn đó

Parameter:   

  • Cú pháp của pathlib.path A path-like object representing a empty directory path. A path-like object is either a string or bytes object representing a path. 

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào. This method does not return any value. This method does not return any value.

Ví dụ: Xóa một thư mục trống bằng rmdir ()

Trong ví dụ này, chúng tôi sẽ xóa một thư mục trống, chúng tôi chỉ cần chỉ định tên thư mục nếu nó nằm trong thư mục gốc

Python3

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
40
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
04
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
08
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
10
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
11
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
48
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
43
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
14
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
68
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
69
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
17
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
71
from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
19

Output:

from pathlib import Path

# .home() is new in 3.5, otherwise use os.path.expanduser('~')
directory_path = Path.home() / 'directory'
directory_path.mkdir()

file_path = directory_path / 'file'
file_path.touch()
1Làm thế nào để bạn xóa một tệp hoặc thư mục trong Python?

Làm thế nào để bạn xóa một tệp hoặc thư mục trong Python?

Xóa một tập tin hoặc thư mục trong Python...

hệ điều hành. Xóa () Xóa một tệp ..

hệ điều hành. Hủy liên kết () xóa một tệp. nó là một tên UNIX của phương thức Remove () ..

giao thoa. rmtree () xóa một thư mục và tất cả các nội dung của nó ..

Pathlib. Đường dẫn. Hẹn phát hành () Xóa một tệp Một mô -đun pathlib có sẵn trong Python 3.4 trở lên ..

Làm cách nào để xóa một thư mục đầy đủ trong Python?

Xóa thư mục (thư mục) trong Python bạn có thể sử dụng hệ điều hành. rmdir () và pathlib. Đường dẫn. rmdir () để xóa một thư mục trống và im lặng.

Làm cách nào để xóa một thư mục?

Để xóa một thư mục và tất cả các nội dung của nó, bao gồm mọi thư mục con và tệp, hãy sử dụng lệnh RM với tùy chọn đệ quy, -r.Các thư mục được xóa bằng lệnh RMDIR không thể được phục hồi, các thư mục cũng không thể bị xóa nội dung của chúng bằng lệnh RM -R.use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

Làm thế nào để bạn xóa một tệp trong Python?

Sử dụng mô -đun OS trong Python để sử dụng mô -đun OS để xóa tệp, chúng tôi nhập nó, sau đó sử dụng hàm Remove () do mô -đun cung cấp để xóa tệp.Nó lấy đường dẫn tệp làm tham số.Bạn không thể chỉ xóa một tệp mà còn là một thư mục sử dụng mô -đun HĐH.import it, then use the remove() function provided by the module to delete the file. It takes the file path as a parameter. You can not just delete a file but also a directory using the os module.import it, then use the remove() function provided by the module to delete the file. It takes the file path as a parameter. You can not just delete a file but also a directory using the os module.