I have some files in directories:
Directory A:
90_1.txt
90_2.txt
90_3.txt
15_1.txt
15_2.txt
In Directory B I have the same filenames with different data
How can I copy data from folder A to folder B by file name? So from folder A: I want to copy the data from the file 90_1.txt to the file 90_1.txt in the folder B I want to copy the data from the file 90_2.txt to the file 90_2.txt in the folder B
import os
import re
import glob
import shutil
from collections import defaultdict
folderPath = r'C:/Users/admin/Desktop/A'
folder_path = r'C:/Users/admin/Desktop/B'
if os.path.exists(folderPath):
files = []
for name in os.listdir(folderPath):
if os.path.isfile(os.path.join(folderPath, name)):
files.append(os.path.join(folderPath, name))
print(files)
# Gather files into groups
groups = defaultdict(set)
for filename in glob.glob(os.path.join(folder_path, "*.txt")):
prefix = os.path.basename(filename).split("_")[0]
groups[prefix].add(filename)
print(groups)
...
Anyone have an idea how to do this?
CodePudding user response:
I think you are looking for shutil.copy
function. Just specify the path of your source folder and destination folder. The piece of code below will list all files in the source folder and copy them to the destination folder.
import os
import shutil
if __name__ == "__main__":
path_dir_src = "C:/Users/admin/Desktop/A/"
path_dir_dst = "/C:/Users/admin/Desktop/B/"
for filename in os.listdir(path_dir_src):
path_absolute_src = os.path.join(path_dir_src, filename)
path_absolute_dst = os.path.join(path_dir_dst, filename)
shutil.copy(src=path_absolute_src, dst=path_absolute_dst)
CodePudding user response:
Another solution will be like this just using OS:
import os
folderPath = r'C:/Users/admin/Desktop/A'
folder_path = r'C:/Users/admin/Desktop/B'
def copy():
if os.path.exists(folderPath):
for name in os.listdir(folderPath):
if os.path.isfile(os.path.join(folderPath, name)):
#create new file or edit existing file for B
with open(f"{folder_path}\{name}", 'w') as file: #use 'a' if you want to merge all contents of both files.
with open(f"{folderPath}\{name}", "r" ) as f: #copy A file content
content = f.read()
file.write(content)
file.close()
copy()
'w':open for writing, truncating the file first
'a':open for writing, appending to the end of file if it exists
Reference: https://docs.python.org/3/library/functions.html#open
CodePudding user response:
If you want to copy a file from src to destination only if it exists in the destination folder use the function below.
import os
import shutil
path_files_A = os.listdir('A')
path_files_B = os.listdir('B')
absolute_path = os.path.abspath(os.getcwd())
def generate_path(folder_category, folder_name):
global absolute_path
return absolute_path r'\{}\{}'.format(folder_category, folder_name)
if __name__ == '__main__':
for path_file_A in path_files_A:
if path_file_A in path_files_B:
absolute_path_of_A = generate_path(folder_category='A', folder_name=path_file_A)
absolute_path_of_B = generate_path(folder_category='B', folder_name=path_file_A)
shutil.copy(absolute_path_of_A, absolute_path_of_B)
In case you want to copy all files, remove the if statement.