0

I have a directory C:/newdir/ It contains the following files with filenames :

s1_student1_file
t1_teacher2_file
hab_parent5_file
y1_professor_file 
bsa_assistant2_file
t1_student_file
nas_officer_file
ee1_newguy15_file
ee1_professor15_file
f1_student8_file

I want to merge the content of s1_student1_file, t1_teacher2_file, t1_student_file, y1_professor_file, ee1_newguy15_file, f1_student8_file, and ee1_professor15_file into a new file called all_file and deleting the files that have been merged from the directory I have to write a python code for this but cannot figure how.

2
  • are they text files? Commented Jun 11, 2020 at 18:15
  • yes they are text files Commented Jun 11, 2020 at 18:16

2 Answers 2

1

You can use glob to list all the chosen text files in a chosen folder. Then, you can use a for-loop to loop over all the text file and write the content into another file:

from glob import glob

with open('all_file.txt','a') as f:
    for file in glob('s1*')+glob('t1*')+glob('y1*')+glob('ee1*'):
        with open(file+'txt','r') as r:
            f.write(r.read())

To remove the files afterwards:

from glob import glob
import os
with open('all_file.txt','a') as f:
    for file in glob('s1*')+glob('t1*')+glob('y1*')+glob('ee1*'):
        with open(file,'r') as r:
            f.write(r.read())
        os.remove(file)
Sign up to request clarification or add additional context in comments.

10 Comments

sorry just one more thing, where in the code should I pass my directory as the argument?
You can change glob('s1*')+glob('t1*')+glob('y1*')+glob('ee1*') to ["C:/newdir/"+file for file in glob('s1*')+glob('t1*')+glob('y1*')+glob('ee1*')].
hi so how would I remove all the files that have been merged from the directory after merging them in the same code
Simply add os.remove(file+'.txt') right under the context manager..
hi the code is running and doesnt show any error but the files arent getting deleted
|
0

Import shutil & pathlib libraries

You can install the libraries using this command –

pip install shutil

pip install pathlib

Python Implementation

import shutil 
from pathlib import Path 

firstfile = Path(r'C:\Users\Sohom\Desktop\A.txt') 
secondfile = Path(r'C:\Users\Sohom\Desktop\B.txt') 

newfile = input("Enter the name of the new file: ") 
print() 
print("The merged content of the 2 files will be in", newfile) 

with open(newfile, "wb") as wfd: 

for f in [firstfile, secondfile]: 
    with open(f, "rb") as fd: 
        shutil.copyfileobj(fd, wfd, 1024 * 1024 * 10) 

  print("\nThe content is merged successfully.!") 
 print("Do you want to view it ? (y / n): ") 

 check = input() 
 if check == 'n': 
exit() 
else: 
print() 
c = open(newfile, "r") 
print(c.read()) 
c.close()

......

Create Array of text files dynamically

2 Comments

Ann Zen's code does the job but I am having difficulty deleting the files that have to be merged from the directory after theyve been merged. Could you help me with that
import os //Call this in loop using file array file_path = '/tmp/file.txt' os.remove(file_path)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.