Phương pháp nào được sử dụng để xóa một tệp trong Python?

Hướng dẫn này giải thích cách xóa các tệp và thư mục bằng cách sử dụng các hàm từ các mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
0,
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
1 và
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
2

Xóa tập tin

Trong Python, bạn có thể sử dụng

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
3,
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
4,
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
5 để xóa một tệp

Mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
0 cung cấp một cách di động để tương tác với hệ điều hành. Mô-đun có sẵn cho cả Python 2 và 3

Để xóa một tệp duy nhất có

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
3, hãy chuyển đường dẫn đến tệp làm đối số

________số 8_______

Hàm

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
3 và
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
4 giống hệt nhau về mặt ngữ nghĩa

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)

Nếu tệp được chỉ định không tồn tại, lỗi

import os

file_path = '/tmp/file.txt'

try:
    os.remove(file_path)
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
0 sẽ xuất hiện. Cả
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
3 và
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
4 chỉ có thể xóa tệp, không xóa thư mục. Nếu đường dẫn đã cho trỏ đến một thư mục, chúng sẽ gặp lỗi
import os

file_path = '/tmp/file.txt'

try:
    os.remove(file_path)
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
3

Xóa tệp cần có quyền ghi và thực thi trên thư mục chứa tệp. Nếu không, bạn sẽ gặp lỗi

import os

file_path = '/tmp/file.txt'

try:
    os.remove(file_path)
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
4

Để tránh lỗi khi xóa tệp, bạn có thể sử dụng xử lý ngoại lệ để bắt ngoại lệ và gửi thông báo lỗi thích hợp

import os

file_path = '/tmp/file.txt'

try:
    os.remove(file_path)
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))

Mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
1 có sẵn trong Python 3. 4 trở lên. Nếu bạn muốn sử dụng mô-đun này trong Python 2, bạn có thể cài đặt nó bằng pip.
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
1 cung cấp giao diện hướng đối tượng để làm việc với các đường dẫn hệ thống tệp cho các hệ điều hành khác nhau

Để xóa một tệp có mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
1, hãy tạo một đối tượng
import os

file_path = '/tmp/file.txt'

try:
    os.remove(file_path)
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
8 trỏ đến tệp và gọi phương thức
import os

file_path = '/tmp/file.txt'

try:
    os.remove(file_path)
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
9 trên đối tượng

from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
5,
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
3, và
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
4 cũng có thể được sử dụng để xóa một liên kết tượng trưng

khớp mẫu

Bạn có thể sử dụng mô-đun toàn cầu để khớp nhiều tệp dựa trên mẫu. Ví dụ: để xóa tất cả các tệp

from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
3 trong thư mục
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
4, bạn sẽ sử dụng một cái gì đó như thế này

import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))

Để xóa đệ quy tất cả các tệp

from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
3 trong thư mục
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
4 và tất cả các thư mục con bên dưới nó, hãy chuyển đối số
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
7 cho hàm
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
8 và sử dụng mẫu ``**`

import os
import glob

files = glob.glob('/tmp/**/*.txt', recursive=True)

for f in files:
    try:
        os.remove(f)
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))

Mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
1 bao gồm hai hàm toàn cầu,
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
8 và
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
1 để khớp các tệp trong một thư mục nhất định.
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
8 chỉ khớp với các tệp trong thư mục cấp cao nhất.
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
1 khớp với tất cả các tệp trong thư mục và tất cả các thư mục con, theo cách đệ quy. Mã ví dụ sau xóa tất cả các tệp
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
3 trong thư mục
from pathlib import Path

file_path = Path('/tmp/file.txt')

try:
    file_path.unlink()
except OSError as e:
    print("Error: %s : %s" % (file_path, e.strerror))
4

from pathlib import Path

for f in Path('/tmp').glob('*.txt'):
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))

Xóa thư mục (Folders)

Trong Python, bạn có thể sử dụng

import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
6 và
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
7 để xóa một thư mục trống và
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
8 để xóa một thư mục không trống

Ví dụ sau đây cho thấy cách xóa một thư mục trống

import os

dir_path = '/tmp/img'

try:
    os.rmdir(dir_path)
except OSError as e:
    print("Error: %s : %s" % (dir_path, e.strerror))

Ngoài ra, bạn có thể xóa các thư mục bằng mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
1

from pathlib import Path

dir_path = Path('/tmp/img')

try:
    dir_path.rmdir()
except OSError as e:
    print("Error: %s : %s" % (dir_path, e.strerror))

Mô-đun

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
2 cho phép bạn thực hiện một số thao tác cấp cao trên tệp và thư mục

Với chức năng

import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
8, bạn có thể xóa một thư mục nhất định bao gồm cả nội dung của nó

import shutil

dir_path = '/tmp/img'

try:
    shutil.rmtree(dir_path)
except OSError as e:
    print("Error: %s : %s" % (dir_path, e.strerror))

Đối số được chuyển đến

import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
8 không thể là một liên kết tượng trưng đến một thư mục

Phần kết luận

Python cung cấp một số mô-đun để xử lý tệp

Chúng tôi đã chỉ cho bạn cách sử dụng

import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
3,
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
4,
import os

file_path = '/tmp/file.txt'
os.unlink(file_path)
5 để xóa một tệp,
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
6 và
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
7 để xóa một thư mục trống và
import os
import glob

files = glob.glob('/tmp/*.txt')

for f in files:
    try:
        f.unlink()
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
8 để xóa đệ quy một thư mục và tất cả nội dung của nó

Hãy hết sức cẩn thận khi xóa tệp hoặc thư mục, vì một khi tệp đã bị xóa thì không thể khôi phục dễ dàng

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

Có nhiều cách để Xóa tệp trong Python nhưng những cách tốt nhất là như sau. .
hệ điều hành. remove() xóa một tập tin
hệ điều hành. unlink() xóa một tập tin. nó là tên Unix của phương thức remove()
đóng cửa. rmtree() xóa một thư mục và tất cả nội dung của nó
đường dẫn. Con đường

Chức năng nào được sử dụng để xóa tệp Python?

remove() trong Python được sử dụng để xóa hoặc xóa đường dẫn tệp.

Để xóa tệp * ta dùng phương pháp nào?

delete() . Trong Java, chúng ta có thể xóa một tệp bằng cách sử dụng lệnh Tệp. phương thức delete() của lớp File. Phương thức xóa () xóa tệp hoặc thư mục được biểu thị bằng tên đường dẫn trừu tượng.

Là xóa một phương pháp trong Python?

Định nghĩa và cách sử dụng . Các yêu cầu XÓA được thực hiện để xóa tài nguyên đã chỉ định (tệp, bản ghi, v.v.). The delete() method sends a DELETE request to the specified url. DELETE requests are made for deleting the specified resource (file, record etc).