Chúng ta sẽ xem xét một số tùy chọn khác nhau mà bạn có để di chuyển tệp xung quanh bằng Python. Ngoài ra còn có một ví dụ nhanh về cách bạn có thể sử dụng thư viện
0 vàimport shutil shutil.move[old_path, new_path]
Learn Data Science with
0 để dọn dẹp thư mục tải xuống của mình trong phần đầu tiên. Vì vậy, nếu bạn là người cần một thứ như thế trong đời, thì hãy tiếp tục đọcimport shutil shutil.move[old_path, new_path]
Learn Data Science with
lựa chọn 1. đóng cửa. di chuyển[]
Ví dụ hiển thị trong phần giới thiệu sử dụng hàm
1 từ thư việnimport shutil shutil.move[old_path, new_path]
Learn Data Science with
0. Chức năng này thực hiện những gì bạn mong đợi và di chuyển các tệp từ vị trí này sang vị trí khác, như sauimport shutil shutil.move[old_path, new_path]
Learn Data Science with
import shutil shutil.move[old_path, new_path]
Learn Data Science with
2 hoạt động bằng cách trước tiên tạo một bản sao của tệp với đường dẫn được xác định bởiimport shutil shutil.move[old_path, new_path]
Learn Data Science with
4 và lưu trữ bản sao ở vị trí mới,import shutil shutil.move[old_path, new_path]
Learn Data Science with
5. Cuối cùng, sau khi tạo thành công bản sao, Python sẽ xóa tệp gốc nằm ởimport shutil shutil.move[old_path, new_path]
Learn Data Science with
4import shutil shutil.move[old_path, new_path]
Learn Data Science with
Trong trường hợp tệp gốc được bảo vệ,
2 sẽ tạo một bản sao của tệp ở vị trí mới, nhưng Python sẽ không thể xóa tệp gốcimport shutil shutil.move[old_path, new_path]
Learn Data Science with
Hầu hết mọi người có các thư mục tải xuống khá lộn xộn. Vì vậy, hãy xem một ví dụ thực tế về cách chúng ta có thể sử dụng
2 để lưu trữ tất cả các hình ảnh trong một thư mục tải xuống trong một thư mục mới có tên làimport shutil shutil.move[old_path, new_path]
Learn Data Science with
9import shutil shutil.move[old_path, new_path]
Learn Data Science with
import os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
Chạy tập lệnh này bên trong thư mục tải xuống sẽ di chuyển bất kỳ tệp nào có phần mở rộng
0 hoặcimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
1 trong thư mục sang thư mụcimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
2. Sử dụngimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
3 trả về danh sách tất cả các tệp trong thư mục. Sau đó, sử dụngimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
4, thư mụcimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
2 được tạo. Sử dụngimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
2, Python sau đó có thể di chuyển tất cả các tệp trong danh sáchimport shutil shutil.move[old_path, new_path]
Learn Data Science with
7 của chúng tôi sang thư mục mới. Quá trình này được thể hiện trong sơ đồ dưới đâyimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
Learn Data Science with
8 để xem thư mục2 có tồn tại trước khi chạyimport os import shutil images = [f for f in os.listdir[] if '.jpg' in f.lower[]] os.mkdir['downloaded_images'] for image in images: new_path = 'downloaded_images/' + image shutil.move[image, new_path]
Learn Data Science with
70 không. Cũng không có lý do gì chúng tôi không thể mở rộng tập lệnh này để tạo các thư mục riêng cho tệp PDF, tệp thực thi và bất kỳ thứ gì khác nằm trong thư mục tải xuống của bạn.import shutil shutil.move[old_path, new_path]
Learn Data Science with
Lựa chọn 2. hệ điều hành. đổi tên[]
Thư viện
0 cũng có một số tùy chọn để di chuyển tệp, một trong số đó làimport shutil shutil.move[old_path, new_path]
Learn Data Science with
72. Chức năng củaimport shutil shutil.move[old_path, new_path]
Learn Data Science with
72 hơi khác một chút so vớiimport shutil shutil.move[old_path, new_path]
Learn Data Science with
2import shutil shutil.move[old_path, new_path]
Learn Data Science with
Thay vì sao chép tệp được đề cập,
75 thay đổi đường dẫn của tệp, tự động thay đổi vị trí tệp. Xem bên dưới để biết ví dụ về cách chúng tôi có thể áp dụng chức năngimport shutil shutil.move[old_path, new_path]
Learn Data Science with
7import shutil shutil.move[old_path, new_path]
Learn Data Science with
76 cũng hoạt động. Mặc dù chức năng được gọi làimport shutil shutil.move[old_path, new_path]
Learn Data Science with
77, nhưng nó cũng di chuyển các tệp bằng cách đổi tên chúng.import shutil shutil.move[old_path, new_path]
Learn Data Science with
76 có thể được triển khai với một mẫu giống hệt vớiimport shutil shutil.move[old_path, new_path]
Learn Data Science with
2 vàimport shutil shutil.move[old_path, new_path]
Learn Data Science with
72import shutil shutil.move[old_path, new_path]
Learn Data Science with
7import shutil shutil.move[old_path, new_path]
Learn Data Science with
76 vàimport shutil shutil.move[old_path, new_path]
Learn Data Science with
72 đều có thể được sử dụng để thay đổi tên tệp hoặc thư mục.import shutil shutil.move[old_path, new_path]
Learn Data Science with
72 báo cáo lỗi khác nhau tùy thuộc vào hệ điều hành bạn đang chạyimport shutil shutil.move[old_path, new_path]
Learn Data Science with
Trong khi đó,
76 sẽ báo cáo lỗi thống nhất trên các hệ thống khác nhau, đây có thể là lựa chọn tốt hơn khi làm việc trên một chương trình cần khả năng tương thích với các hệ điều hành khác nhauimport shutil shutil.move[old_path, new_path]
Learn Data Science with
Tùy chọn 3. đường dẫn. Con đường[]. đổi tên[]
Đối với cách tiếp cận hướng đối tượng hơn để di chuyển tệp,
75 cũng là một tùy chọnimport shutil shutil.move[old_path, new_path]
Learn Data Science with
Bằng cách sử dụng hàm
76, Python tạo một đối tượngimport shutil shutil.move[old_path, new_path]
Learn Data Science with
77. Phương thứcimport shutil shutil.move[old_path, new_path]
Learn Data Science with
75 sau đó thay đổi đường dẫn của đối tượng, tương tự như cách hoạt động củaimport shutil shutil.move[old_path, new_path]
Learn Data Science with
72import shutil shutil.move[old_path, new_path]
Learn Data Science with
7import shutil shutil.move[old_path, new_path]
Learn Data Science with
Chúng tôi cũng có thể áp dụng
70 cho tập lệnh được tạo trước đó để di chuyển hình ảnh ra khỏi thư mụcimport shutil shutil.move[old_path, new_path]
Learn Data Science with
71 của chúng tôi. Xem bên dưới để biết ví dụ về điều nàyimport shutil shutil.move[old_path, new_path]
Learn Data Science with
0import shutil shutil.move[old_path, new_path]
Learn Data Science with
Dưới đây là bảng so sánh sự khác biệt về tốc độ của ba cách tiếp cận
1import shutil shutil.move[old_path, new_path]
Learn Data Science with
Ngoài
gõmicroseconds1os. đổi tên[]50. 550shut. di chuyển[]54. 582Pathlib. Đường dẫn[]63. 74
Tóm lược
Bạn có một số tùy chọn khi di chuyển tệp xung quanh.
2,import shutil shutil.move[old_path, new_path]
Learn Data Science with
72 vàimport shutil shutil.move[old_path, new_path]
Learn Data Science with
76 đều là những cách tiếp cận tuyệt vời, với cả ba cách tiếp cận này đều sử dụng các đối sốimport shutil shutil.move[old_path, new_path]
Learn Data Science with
75. Đối với cách tiếp cận hướng đối tượng hơn, bạn cũng có thể sử dụngimport shutil shutil.move[old_path, new_path]
Learn Data Science with
70, cung cấp choimport shutil shutil.move[old_path, new_path]
Learn Data Science with
76 vớiimport shutil shutil.move[old_path, new_path]
Learn Data Science with
4 vàimport shutil shutil.move[old_path, new_path]
Learn Data Science with
75 vớiimport shutil shutil.move[old_path, new_path]
Learn Data Science with
5import shutil shutil.move[old_path, new_path]
Learn Data Science with
Bắt đầu học miễn phí
Nhận thông tin cập nhật trong hộp thư đến của bạn
Tham gia cùng hơn 7.500 người học khoa học dữ liệu
Bài viết gần đây
Ưu đãi khóa học tốt nhất cho Thứ Sáu Đen và Thứ Hai Điện Tử 2022Hàm sigmoidchấm sản phẩm7 khóa học về trí tuệ nhân tạo [AI] tốt nhất năm 2022Các khóa học Python tốt nhất theo phân tích dữ liệu
Trong số khoảng 3000 dịch vụ, đây là những khóa học Python tốt nhất theo phân tích này
xem bài viếtNhận thông tin cập nhật trong hộp thư đến của bạn
Tham gia cùng hơn 7.500 người học khoa học dữ liệu
Gặp gỡ các tác giả
Nhà khoa học dữ liệu Alfie Grace
Alfie tốt nghiệp Thạc sĩ Kỹ thuật Cơ khí tại Đại học College London. Anh ấy hiện đang làm Nhà khoa học dữ liệu tại Square Enix. Tìm anh ấy trên LinkedIn