如何使用 python 将多个文件夹中的多个文件复制到一个文件夹中?
How to copy multiple files from multiple folders into one folder using python?
我的文件结构如图。我总共有 9 个文件夹,每个文件夹包含 130 个视频文件。我想将所有 9 个文件夹中的第一个视频文件移动到一个新文件夹“文件夹 01”中,依此类推。
因为,所有文件夹的第一个视频文件将具有相同的名称,我该如何解决这个问题?我需要一些见解来解决这个问题?
y 可以使用 shutil
import shutil
original = r'C:\Users\Ron\Desktop\Test_1\products.csv'
target = r'C:\Users\Ron\Desktop\Test_2\products.csv'
shutil.copyfile(original, target)
我可以给你提示!不要犹豫,要求澄清
def locate_path_by_substring(directory, substring, list_paths):
for r, d, f in os.walk(directory):
for myfile in f:
if (substring+".log") in myfile:
list_paths.append(os.path.join(r, myfile))
return list_paths
`
def copy_file(original,target):
shutil.copyfile(original, target)
def create_direct(path):
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
def main():
for i in range (1,131):
locate_path_by_substring(directory, i, list_file_{}.format(i))
create_direct(directory+"/folder_{}".format(i))
for i in range (1,131):
for j in range (0,9):
copy_file(list_file_{i}[j],directory+"/folder_{}".format(i)/)`
Python 有一个叫做 os.walk()
的很棒的函数,它可以为你做很多这样的事情。
你可以做到
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print(file)
这将打印目录和子目录中的所有文件,然后使用它您可以执行检查并使用 os.rename
相应地重命名文件。
以下代码将执行该任务。我假设目标路径的基本文件夹与当前路径相同。
#!/usr/bin/env python3
import os
import shutil
base_folder = os.getcwd()
# Create 130 destination folders (folder_001, ..., folder_130)
dest_folder_prefix = "folder_"
dest_path = base_folder # An assumption
dest_paths = {}
for n in range(1, 131):
folder_number = str(n).zfill(3)
dest_folder = os.path.join(dest_path, f'{dest_folder_prefix}{folder_number}')
if not os.path.isdir(dest_folder):
os.mkdir(dest_folder)
# Store paths for quick lookup later
dest_paths[n] = dest_folder
# Iterate through the existing folders
src_folders = sorted([f for f in os.listdir(base_folder) if os.path.isdir(f)])
for n, folder_name in enumerate(src_folders, 1):
folder_number = str(n).zfill(2) # Used to rename destination file
folder_path = os.path.join(base_folder, folder_name)
# Process the list of files
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
for file_name in files:
name, ext = os.path.splitext(file_name) # e.g. '1', '.avi' for file '1.avi'
file_number = int(name) # file name is a number
# Set the source
src_path = os.path.join(folder_path, file_name)
# Set the destination filename and path
dest_filename = f'{name}_{folder_number}{ext}' # Destination file name e.g. '1_01.avi'
dest_filepath = os.path.join(dest_paths[file_number], dest_filename)
# Perform the move operation
shutil.move(src_path, dest_filepath)
我的文件结构如图。我总共有 9 个文件夹,每个文件夹包含 130 个视频文件。我想将所有 9 个文件夹中的第一个视频文件移动到一个新文件夹“文件夹 01”中,依此类推。 因为,所有文件夹的第一个视频文件将具有相同的名称,我该如何解决这个问题?我需要一些见解来解决这个问题?
y 可以使用 shutil
import shutil
original = r'C:\Users\Ron\Desktop\Test_1\products.csv'
target = r'C:\Users\Ron\Desktop\Test_2\products.csv'
shutil.copyfile(original, target)
我可以给你提示!不要犹豫,要求澄清
def locate_path_by_substring(directory, substring, list_paths):
for r, d, f in os.walk(directory):
for myfile in f:
if (substring+".log") in myfile:
list_paths.append(os.path.join(r, myfile))
return list_paths
`
def copy_file(original,target):
shutil.copyfile(original, target)
def create_direct(path):
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
def main():
for i in range (1,131):
locate_path_by_substring(directory, i, list_file_{}.format(i))
create_direct(directory+"/folder_{}".format(i))
for i in range (1,131):
for j in range (0,9):
copy_file(list_file_{i}[j],directory+"/folder_{}".format(i)/)`
Python 有一个叫做 os.walk()
的很棒的函数,它可以为你做很多这样的事情。
你可以做到
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print(file)
这将打印目录和子目录中的所有文件,然后使用它您可以执行检查并使用 os.rename
相应地重命名文件。
以下代码将执行该任务。我假设目标路径的基本文件夹与当前路径相同。
#!/usr/bin/env python3
import os
import shutil
base_folder = os.getcwd()
# Create 130 destination folders (folder_001, ..., folder_130)
dest_folder_prefix = "folder_"
dest_path = base_folder # An assumption
dest_paths = {}
for n in range(1, 131):
folder_number = str(n).zfill(3)
dest_folder = os.path.join(dest_path, f'{dest_folder_prefix}{folder_number}')
if not os.path.isdir(dest_folder):
os.mkdir(dest_folder)
# Store paths for quick lookup later
dest_paths[n] = dest_folder
# Iterate through the existing folders
src_folders = sorted([f for f in os.listdir(base_folder) if os.path.isdir(f)])
for n, folder_name in enumerate(src_folders, 1):
folder_number = str(n).zfill(2) # Used to rename destination file
folder_path = os.path.join(base_folder, folder_name)
# Process the list of files
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
for file_name in files:
name, ext = os.path.splitext(file_name) # e.g. '1', '.avi' for file '1.avi'
file_number = int(name) # file name is a number
# Set the source
src_path = os.path.join(folder_path, file_name)
# Set the destination filename and path
dest_filename = f'{name}_{folder_number}{ext}' # Destination file name e.g. '1_01.avi'
dest_filepath = os.path.join(dest_paths[file_number], dest_filename)
# Perform the move operation
shutil.move(src_path, dest_filepath)