Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Tôi muốn di chuyển tất cả các tệp văn bản từ một thư mục sang một thư mục khác bằng Python. Tôi đã tìm thấy mã này:

import os, shutil, glob

dst = '/path/to/dir/Caches/com.apple.Safari/WebKitCache/Version\ 4/Blobs '
try:
    os.makedirs(/path/to/dir/Tumblr/Uploads) # create destination directory, if needed (similar to mkdir -p)
except OSError:
    # The directory already existed, nothing to do
    pass

for txt_file in glob.iglob('*.txt'):
    shutil.copy2(txt_file, dst)

Tôi muốn nó di chuyển tất cả các tệp trong thư mục

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
5. Tôi không gặp lỗi, nhưng nó cũng không di chuyển các tệp.

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Aran-Fey

37K11 Huy hiệu vàng97 Huy hiệu bạc141 Huy hiệu Đồng11 gold badges97 silver badges141 bronze badges

Hỏi ngày 24 tháng 1 năm 2017 lúc 11:18Jan 24, 2017 at 11:18

Thử cái này:

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Đã trả lời ngày 24 tháng 1 năm 2017 lúc 11:36Jan 24, 2017 at 11:36

Shivkumar Kondishivkumar KondiShivkumar kondi

6.1088 Huy hiệu vàng29 Huy hiệu bạc56 Huy hiệu Đồng8 gold badges29 silver badges56 bronze badges

5

Supered điều này không có câu trả lời bằng cách sử dụng pathilib được giới thiệu trong Python ________ 16+

Ngoài ra, SHOTIL được cập nhật trong Python

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
7 để chấp nhận đối tượng Pathlib Chi tiết chi tiết trong PEP-0519 này

Pathlib

from pathlib import Path

src_path = '\tmp\files_to_move'

for each_file in Path(src_path).glob('*.*'): # grabs all files
    trg_path = each_file.parent.parent # gets the parent of the folder 
    each_file.rename(trg_path.joinpath(each_file.name)) # moves to parent folder.

Pathlib & Shutil để sao chép tệp.

from pathlib import Path
import shutil

src_path = '\tmp\files_to_move'
trg_path = '\tmp'

for src_file in Path(src_path).glob('*.*'):
    shutil.copy(src_file, trg_path)

Đã trả lời ngày 5 tháng 4 năm 2020 lúc 13:13Apr 5, 2020 at 13:13

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Umar.HUmar.HUmar.H

21.7K6 Huy hiệu vàng32 Huy hiệu bạc64 Huy hiệu đồng6 gold badges32 silver badges64 bronze badges

4

Xin vui lòng, hãy xem thực hiện chức năng copytree trong đó:

  • Liệt kê các tệp thư mục với:

    import shutil
    import os
        
    source_dir = '/path/to/source_folder'
    target_dir = '/path/to/dest_folder'
        
    file_names = os.listdir(source_dir)
        
    for file_name in file_names:
        shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    8

  • Sao chép tệp với:

for name in names:
  srcname = os.path.join(src, name)
  dstname = os.path.join(dst, name)
  copy2(srcname, dstname)

Nhận DSTName là không cần thiết, bởi vì nếu tham số đích chỉ định một thư mục, tệp sẽ được sao chép vào DST bằng tên tệp cơ sở từ srcname.

Thay thế COPY2 bằng cách di chuyển.

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Pat Myron

4.1882 Huy hiệu vàng21 Huy hiệu bạc39 Huy hiệu Đồng2 gold badges21 silver badges39 bronze badges

Đã trả lời ngày 24 tháng 1 năm 2017 lúc 11:44Jan 24, 2017 at 11:44

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Sao chép tệp ".txt" từ thư mục này sang thư mục khác rất đơn giản và câu hỏi chứa logic. Chỉ phần còn thiếu được thay thế bằng thông tin đúng như dưới đây:

import os, shutil, glob

src_fldr = r"Source Folder/Directory path"; ## Edit this

dst_fldr = "Destiantion Folder/Directory path"; ## Edit this

try:
  os.makedirs(dst_fldr); ## it creates the destination folder
except:
  print "Folder already exist or some error";

Dưới đây các dòng mã sẽ sao chép tệp với các tệp tiện ích *.txt từ src_fldr sang dst_fldr

for txt_file in glob.glob(src_fldr+"\\*.txt"):
    shutil.copy2(txt_file, dst_fldr);

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Đã trả lời ngày 24 tháng 1 năm 2017 lúc 12:00Jan 24, 2017 at 12:00

TousiftousifToUsIf

Phù hiệu bằng đồng 16122 bronze badges

Cái này cần phải dùng mẹo. Đồng thời đọc tài liệu của mô -đun SHOTIL để chọn hàm phù hợp với nhu cầu của bạn (Sould.Copy (), Sould.Copy2 (), Sould.CopyFile () hoặc SHOWIL.Move ()).

import glob, os, shutil

source_dir = '/path/to/dir/with/files' #Path where your files are at the moment
dst = '/path/to/dir/for/new/files' #Path you want to move your files to
files = glob.iglob(os.path.join(source_dir, "*.txt"))
for file in files:
    if os.path.isfile(file):
        shutil.copy2(file, dst)

Đã trả lời ngày 24 tháng 1 năm 2017 lúc 11:29Jan 24, 2017 at 11:29

TehctehcTehc

6693 Huy hiệu vàng9 Huy hiệu bạc28 Huy hiệu đồng3 gold badges9 silver badges28 bronze badges

1

import shutil 
import os 
import logging

source = '/var/spools/asterisk/monitor' 
dest1 = '/tmp/'


files = os.listdir(source)

for f in files:
        shutil.move(source+f, dest1)

logging.basicConfig(filename='app.log', filemode='w', format='%(name)s
- %(levelname)s - %(message)s')

logging.info('directories moved')

Một chút mã nấu chín với tính năng nhật ký. Bạn cũng có thể định cấu hình điều này để chạy vào một khoảng thời gian bằng cách sử dụng crontab.

* */1 * * * python /home/yourprogram.py > /dev/null 2>&1

Chạy mỗi giờ! Chúc mừng

Đã trả lời ngày 17 tháng 1 năm 2020 lúc 12:25Jan 17, 2020 at 12:25

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Daniel Adenewdaniel AdenewDaniel Adenew

7.3637 Huy hiệu vàng54 Huy hiệu bạc75 Huy hiệu Đồng7 gold badges54 silver badges75 bronze badges

Thử cái này:

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
0

Đã trả lời ngày 24 tháng 1 năm 2017 lúc 11:36Mar 5, 2021 at 8:33

Shivkumar Kondishivkumar Kondi

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
1

6.1088 Huy hiệu vàng29 Huy hiệu bạc56 Huy hiệu ĐồngNov 24, 2020 at 1:59

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Supered điều này không có câu trả lời bằng cách sử dụng pathilib được giới thiệu trong Python ________ 16+

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
2

Ngoài ra, SHOTIL được cập nhật trong Python

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
7 để chấp nhận đối tượng Pathlib Chi tiết chi tiết trong PEP-0519 nàyJul 4, 2020 at 19:45

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
3

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Pathlib

Pathlib & Shutil để sao chép tệp.89 gold badges264 silver badges245 bronze badges

Đã trả lời ngày 5 tháng 4 năm 2020 lúc 13:13Jul 14, 2020 at 18:31

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

1

21.7K6 Huy hiệu vàng32 Huy hiệu bạc64 Huy hiệu đồng

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
4

Xin vui lòng, hãy xem thực hiện chức năng copytree trong đó:

Liệt kê các tệp thư mục với:1 gold badge10 silver badges24 bronze badges

Sao chép tệp với:Jun 1 at 8:40

Hướng dẫn python move all files from one directory to another - python di chuyển tất cả các tệp từ thư mục này sang thư mục khác

Làm cách nào để di chuyển nhiều tệp từ thư mục này sang thư mục khác trong Python?

Giả sử bạn muốn di chuyển tất cả/nhiều tệp từ thư mục này sang thư mục khác, sau đó sử dụng hàm os.listdir () để liệt kê tất cả các tệp của thư mục nguồn, sau đó lặp lại danh sách bằng cách sử dụng vòng lặp và di chuyển mỗi tệp bằng cách sử dụng di chuyển () hàm số.use the os. listdir() function to list all files of a source folder, then iterate a list using a for loop and move each file using the move() function.

Làm thế nào để bạn di chuyển tất cả các tệp trong một thư mục sang một thư mục khác trong Python?

Di chuyển () Phương thức di chuyển các tệp trong Python bằng cách sử dụng. Shutil. Phương thức di chuyển () lấy hai đối số đầu tiên là đường dẫn nguồn hoàn chỉnh và hướng thứ hai là đường dẫn đích (bao gồm tên tệp/thư mục để di chuyển), hàm di chuyển sẽ chuyển tệp từ nguồn sang đích. move Files in Python using the. The shutil. move() method takes two arguments first one is the complete source path and the second one is the destination path (including the file/folder name to move), the move function will move the file from source to the destination.

Làm cách nào để sao chép các tệp từ thư mục này sang thư mục khác trong Python?

Một cách nhanh chóng để di chuyển một tập tin từ nơi này sang nơi khác là sử dụng SOWL.MOVE () như được hiển thị:..
nhập khẩu.giao thoa.di chuyển ('old_directory/test_file.txt', 'new_directory/test_file.txt') ....
nhập khẩu.giao thoa.Di chuyển (Old_Path, New_Path) ....
Nhập hệ điều hành.hệ điều hành.....
Nhập hệ điều hành.hệ điều hành.....
Nhập Pathlib.Pathlib ..

Làm cách nào để di chuyển một tệp từ thư mục này sang thư mục khác?

Bạn có thể di chuyển một tệp hoặc thư mục từ thư mục này sang thư mục khác bằng cách kéo nó từ vị trí hiện tại của nó và thả nó vào thư mục đích, giống như bạn làm với một tệp trên máy tính để bàn của bạn.Cây thư mục: Nhấp chuột phải vào tệp hoặc thư mục bạn muốn và từ menu hiển thị nhấp vào MOVE hoặc Sao chép.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.