Hướng dẫn move folder python - di chuyển thư mục python

Bạn có thể sử dụng mẫu sau để di chuyển một tệp trong Python:file in Python:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original, target)

Ngoài ra, bạn có thể sử dụng mẫu này để di chuyển một thư mục:directory:

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)

Bây giờ, hãy xem xét một số ví dụ với các bước để di chuyển tệp hoặc thư mục của bạn bằng Python.

Bước 1: Chụp đường dẫn ban đầu

Để bắt đầu, hãy chụp đường dẫn ban đầu nơi tập tin của bạn hiện đang được lưu trữ.

Ví dụ: hãy giả sử rằng một tệp CSV được lưu trữ trong một thư mục có tên là Test_1:Test_1:

C: \ Users \ Ron \ Desktop \ test_1 \ my_csv_file.csv

Trong đó tên tệp là ‘my_csv_file, và phần mở rộng tệp là CSV.my_csv_file’ and the file extension is csv.

Bước 2: Chụp đường đích

Tiếp theo, chụp đường dẫn đích nơi tệp sẽ được di chuyển.

Ví dụ của chúng tôi, hãy để di chuyển tệp CSV sang một thư mục có tên Test_2:Test_2:

C: \ Users \ Ron \ Desktop \ test_2 \ my_csv_file.csv

Bước 3: Di chuyển tệp bằng Python

Bây giờ bạn có thể sử dụng mẫu này để di chuyển tệp đến vị trí đích:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original, target)

Đảm bảo đặt ký tự ‘R trước mỗi đường dẫn của bạn để tránh lỗi sau:r‘ character before each of your paths to avoid the following error:

‘Unicodeescape, codec có thể giải mã byte ở vị trí 2-3: bị cắt cụt \ uxxxxxxxxx

Ví dụ của chúng tôi, mã để di chuyển tệp CSV từ vị trí ban đầu (nghĩa là, test_1) sang vị trí đích (nghĩa là, test_2) như sau:Test_1) to the target location (i.e., Test_2) is as follows:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_csv_file.csv'
target = r'C:\Users\Ron\Desktop\Test_2\my_csv_file.csv'

shutil.move(original, target)

Khi bạn chạy mã trong Python (được điều chỉnh theo đường dẫn của bạn), tệp CSV sẽ được chuyển đến thư mục Test_2.Test_2 folder.

Đổi tên tệp khi di chuyển nó

Ngoài ra, bạn có thể đổi tên tệp của mình khi bạn di chuyển nó đến vị trí mục tiêu của mình.

Chẳng hạn, hãy để giả sử rằng một tệp JPG mới được lưu trữ trong thư mục Test_1 (trong đó tên tệp là hình ảnh).Test_1 folder (where the file name is image).

Mã bên dưới sau đó có thể được sử dụng để di chuyển tệp (với tên tệp gốc của ‘hình ảnh‘) đến vị trí đích với tên tệp mới (‘new_image‘):image‘) to the target location with a new file name (‘new_image‘):

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\image.jpg'
target = r'C:\Users\Ron\Desktop\Test_2\new_image.jpg'

shutil.move(original, target)

Tệp, với tên mới, bây giờ sẽ xuất hiện trong thư mục Test_2.Test_2 folder.

Di chuyển một thư mục bằng Python

Cho đến nay, bạn đã thấy cách di chuyển một tập tin trong Python.

Ngoài ra, bạn có thể di chuyển một thư mục bằng mẫu này (mà không chỉ định bất kỳ phần mở rộng tệp nào):directory using this template (without specifying any file extension):

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)

Chẳng hạn, hãy để nói rằng một thư mục mới đã được thêm vào vị trí Test_1, trong đó tên thư mục là My_Folder.Test_1 location, where the directory name is my_folder.

Do đó, mã sau có thể được sử dụng để di chuyển thư mục đến vị trí mục tiêu Test_2:Test_2 target location:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_folder'
target = r'C:\Users\Ron\Desktop\Test_2\my_folder'

shutil.move(original, target)

Thư mục bây giờ sẽ xuất hiện dưới vị trí mục tiêu.

Bạn chỉ thấy cách di chuyển một tập tin trong Python bằng cách sử dụng Sould.move. Bạn cũng có thể muốn kiểm tra hướng dẫn sau giải thích cách sao chép một tệp trong Python.

Bạn có thể sử dụng mẫu sau để di chuyển một tệp trong Python:file in Python:

Nội phân Chính showShow

  • Bước 1: Chụp đường dẫn ban đầu
  • Bước 2: Chụp đường đích
  • Bước 3: Di chuyển tệp bằng Python
  • Đổi tên tệp khi di chuyển nó
  • Di chuyển một thư mục bằng Python
  • Bạn có thể di chuyển các tệp với Python không?
  • Làm cách nào để điều hướng từ thư mục này sang thư mục khác?
  • Làm cách nào để chuyển tệp vào một thư mục?

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original, target)

Ngoài ra, bạn có thể sử dụng mẫu này để di chuyển một thư mục:directory:

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)

Bây giờ, hãy xem xét một số ví dụ với các bước để di chuyển tệp hoặc thư mục của bạn bằng Python.

Bước 1: Chụp đường dẫn ban đầu

Bước 2: Chụp đường đích

Bước 3: Di chuyển tệp bằng PythonTest_1:

Đổi tên tệp khi di chuyển nó

Di chuyển một thư mục bằng Pythonmy_csv_file’ and the file extension is csv.

Bước 2: Chụp đường đích

Bước 3: Di chuyển tệp bằng Python

Đổi tên tệp khi di chuyển nóTest_2:

Di chuyển một thư mục bằng Python

Bước 3: Di chuyển tệp bằng Python

Đổi tên tệp khi di chuyển nó

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original, target)

Di chuyển một thư mục bằng Pythonr‘ character before each of your paths to avoid the following error:

Bạn có thể di chuyển các tệp với Python không?

Làm cách nào để điều hướng từ thư mục này sang thư mục khác?Test_1) to the target location (i.e., Test_2) is as follows:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_csv_file.csv'
target = r'C:\Users\Ron\Desktop\Test_2\my_csv_file.csv'

shutil.move(original, target)

Làm cách nào để chuyển tệp vào một thư mục?Test_2 folder.

Đổi tên tệp khi di chuyển nó

Di chuyển một thư mục bằng Python

Bạn có thể di chuyển các tệp với Python không?Test_1 folder (where the file name is image).

Làm cách nào để điều hướng từ thư mục này sang thư mục khác?image‘) to the target location with a new file name (‘new_image‘):

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\image.jpg'
target = r'C:\Users\Ron\Desktop\Test_2\new_image.jpg'

shutil.move(original, target)

Làm cách nào để chuyển tệp vào một thư mục?Test_2 folder.

Di chuyển một thư mục bằng Python

Bạn có thể di chuyển các tệp với Python không?

Làm cách nào để điều hướng từ thư mục này sang thư mục khác?directory using this template (without specifying any file extension):

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)

Làm cách nào để chuyển tệp vào một thư mục?Test_1 location, where the directory name is my_folder.

Ngoài ra, bạn có thể sử dụng mẫu này để di chuyển một thư mục:Test_2 target location:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_folder'
target = r'C:\Users\Ron\Desktop\Test_2\my_folder'

shutil.move(original, target)

Bây giờ, hãy xem xét một số ví dụ với các bước để di chuyển tệp hoặc thư mục của bạn bằng Python.

Để bắt đầu, hãy chụp đường dẫn ban đầu nơi tập tin của bạn hiện đang được lưu trữ.

Ví dụ: hãy giả sử rằng một tệp CSV được lưu trữ trong một thư mục có tên là Test_1:

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)
4

C: \ Users \ Ron \ Desktop \ test_1 \ my_csv_file.csv

Edit:

Trong đó tên tệp là ‘my_csv_file, và phần mở rộng tệp là CSV.

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)
5

Bạn có thể di chuyển các tệp với Python không?

Làm cách nào để điều hướng từ thư mục này sang thư mục khác?. This can be achieved using shutil. move() function from shutil module.

Làm cách nào để điều hướng từ thư mục này sang thư mục khác?

Làm cách nào để chuyển tệp vào một thư mục?dragging it from its current location and dropping it into the destination folder, just as you would with a file on your desktop. Folder Tree: Right-click the file or folder you want, and from the menu that displays click Move or Copy.

Làm cách nào để chuyển tệp vào một thư mục?

Di chuyển tập tin từ phần danh mục..

Trên thiết bị Android của bạn, mở các tệp của Google ..

Ở phía dưới, nhấn Duyệt ..

Trong "Danh mục", chọn một danh mục ..

Tìm các tệp bạn muốn di chuyển. Để di chuyển một tệp: Bên cạnh tệp, nhấn thêm. . ....

Nhấn vào lưu trữ nội bộ ..

Chọn thư mục bạn muốn chuyển tệp sang ..

Nhấn vào Move ở đây ..