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 shutil
và
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
shutil
. 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
_______ 52 _______ 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ởi
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 có tạiimport 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ệ, shutil.move[]
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ốc
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 shutil.move[]
để 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à
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
shutil.move[]
, sau đó Python có thể di chuyển tất cả các tệp trong danh sách 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
0. 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 os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
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
2. Chức năng củaimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
2 hơi khác một chút so vớiimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
shutil.move[]
Thay vì sao chép tệp được đề cập,
5 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 os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
import os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
6 cũng được. Mặc dù chức năng được gọi làimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
7, nhưng nó cũng di chuyển các tệp bằng cách đổi tên chúng.import os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
6 có thể được triển khai với một mẫu giống hệt vớiimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
shutil.move[]
và 2import os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
import os os.replace['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
Cả
6 vàimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
2 đều có thể được sử dụng để thay đổi tên tệp hoặc thư mục.import os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
2 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 os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
Trong khi đó,
6 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 os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
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,
5 cũng là một tùy chọnimport os os.replace['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
Bằng cách sử dụng hàm
6, Python tạo một đối tượngimport os os.replace['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
7. Phương thứcimport os os.replace['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
5 sau đó thay đổi đường dẫn của đối tượng, tương tự như cách hoạt động củaimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
2import os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
import pathlib pathlib.Path['old_directory/test_file.txt'].rename['new_directory/test_file.txt']
Learn Data Science with
Chúng tôi cũng có thể áp dụng
0 cho tập lệnh đã tạo trước đó để di chuyển hình ảnh ra khỏi thư mụcimport pathlib pathlib.Path['old_directory/test_file.txt'].rename['new_directory/test_file.txt']
Learn Data Science with
1 của chúng tôi. Xem bên dưới để biết ví dụ về điều nàyimport pathlib pathlib.Path['old_directory/test_file.txt'].rename['new_directory/test_file.txt']
Learn Data Science with
import os import pathlib 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 pathlib.Path[image].rename[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
import shutil import os import pathlib # Create anonymous functions - need to regenerate files after each move def gen_file[file_name]: with open[f'old_directory/{file_name}', 'w'] as f: f.write[''] def shutil_move[]: gen_file['shutil.txt'] shutil.move['old_directory/shutil.txt', 'new_directory/shutil.txt'] def os_move[]: gen_file['os.txt'] os.rename['old_directory/os.txt', 'new_directory/os.txt'] def pathlib_move[]: gen_file['pathlib.txt'] pathlib.Path['old_directory/pathlib.txt'].rename['new_directory/pathlib.txt'] # Calculate timings shutil_time = %timeit -o -q shutil_move[] os_time = %timeit -o -q os_move[] pathlib_time = %timeit -o -q pathlib_move[] import pandas as pd # Create data table data = [['shutil.move[]', shutil_time.best], ['os.rename[]', os_time.best], ['Pathlib.Path[]', pathlib_time.best]] df = pd.DataFrame[data, columns=['type', 'microseconds']] df.microseconds = round[df.microseconds * 1e6, 2] df.sort_values['microseconds', inplace=True] df
Learn Data Science with
Ngoài
gõmicroseconds1os. đổi tên[]50. 550shut. di chuyển[]54. 582Pathlib. Đường dẫn[]63. 74
Bản tóm tắt
Bạn có một số tùy chọn khi di chuyển tệp xung quanh. shutil.move[]
,
2 vàimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
6 đều là những cách tiếp cận tuyệt vời, với cả ba cách tiếp cận đều sử dụng các lập luậnimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
5. Đố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 pathlib pathlib.Path['old_directory/test_file.txt'].rename['new_directory/test_file.txt']
Learn Data Science with
0, cung cấpimport pathlib pathlib.Path['old_directory/test_file.txt'].rename['new_directory/test_file.txt']
Learn Data Science with
6 vớiimport os os.replace['old_directory/test_file.txt', 'new_directory/test_file.txt']
Learn Data Science with
4 vàimport shutil shutil.move[old_path, new_path]
Learn Data Science with
5 vớiimport os os.rename['old_directory/test_file.txt', 'new_directory/test_file.txt']
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 2023Nhậ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