Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách xóa file bằng Python?


Bạn xóa file đơn lẻ hoặc thư mục rỗng bằng hàm trong module os. Ví dụ xóa file a.txt:

>>> import os
>>> os.remove('a.txt')

Tham số os.remove phải là đường dẫn tuyệt đối hoặc tương đối. Cũng dùng os.unlink() xóa file:

>>> import os
>>> os.unlink('a.txt')