Làm cách nào để lưu tệp vào một thư mục cụ thể trong python?

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

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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 đọc

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

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

1 từ thư viện
import 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ư sau

import 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ởi
import 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

4

Trong trường hợp tệp gốc được bảo vệ,

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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ố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

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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

9

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

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

0 hoặc
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

1 trong thư mục sang thư mục
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

2. Sử dụng
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

3 trả về danh sách tất cả các tệp trong thư mục. Sau đó, sử dụng
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

4, thư mục
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

2 được tạo. Sử dụng
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

2, Python sau đó có thể di chuyển tất cả các tệp trong danh sách
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

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 đây

Learn Data Science with

8 để xem thư mục
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

2 có tồn tại trước khi chạy
import shutil

shutil.move[old_path, 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.

Lựa chọn 2. hệ điều hành. đổi tên[]

Thư viện

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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ủa
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

72 hơi khác một chút so với
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

2

Thay vì sao chép tệp được đề cập,

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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ăng

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

7

import 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ới
import 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

72

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

7

import 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ạy

Trong khi đó,

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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 nhau

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,

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

75 cũng là một tùy chọn

Bằng cách sử dụng hàm

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

76, Python tạo một đối tượng
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

77. Phương thức
import 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ủa
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

72

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

7

Chúng tôi cũng có thể áp dụng

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

70 cho tập lệnh được tạo trước đó để di chuyển hình ảnh ra khỏi thư mục
import 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ày

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

0

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

shutil.move[old_path, new_path]

Learn Data Science with

1

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.

import shutil

shutil.move[old_path, new_path]

Learn Data Science with

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ụng
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

70, cung cấp cho
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

76 với
import 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ới
import shutil

shutil.move[old_path, new_path]

Learn Data Science with

5

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 2022

Cá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ết

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

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

Chủ Đề