krish
krish

Reputation: 67

How to make multiple file from different folder same name in one file in python

I want to combine multiple file from different folder data in one file but only same file name is all folder

Script:

import os

filenames = [os.path.join('C:/Users/Vishnu/Desktop/Test_folder/Input/','*.txt'), os.path.join('C:/Users/Vishnu/Desktop/Test_folder/Output/','*.txt')]
f = open(r'C:/Users/Vishnu/Desktop/Test_output/', 'wb')
for fname in filenames:
    with open(fname) as infile:
        for line in infile:
            f.write(line)

Getting Error:

f = open(r"C:/Users/Vishnu/Desktop/Test_output/", "wb")
IOError: [Errno 13] Permission denied: 'C:/Users/Vishnu/Desktop/Test_output/'
>>> 

After Modification

import os
import glob 
import os.mkdir

filenames = [glob.globos(mkdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' ,'Desktop','Test_folder','Input','*.txt'))), glob.glob(os.mkdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop','Test_folder','Output','*.txt')))]    
filenames[0].extend(filenames[1])
filenames=filenames[0]

if( not os.path.isdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop' ,'Test_folder', 'Test_output'))):
    os.mkdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop' ,'Test_folder', 'Test_output'))
for fname in filenames:
    with open(fname) as file:
        for line in file.readlines():
            f = open(os.mkdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output')),'{:}.txt'.format(os.path.split(fname)[-1] ), 'a+')
            f.write(line)
            f.close()  

Upvotes: 0

Views: 2779

Answers (2)

R. S. Nikhil Krishna
R. S. Nikhil Krishna

Reputation: 4250

Firstly, you are trying to open the folder itself. Secondly, we have to close the file everytime we read it to avoid Permission issues

I tried this code. It should work now

import os
import glob    #So that * in directory listing can be interpretted as all filenames

filenames = [glob.glob(os.path.join(os.path.expanduser('~'),'Desktop','Test_folder','Input','*.txt')), glob.glob(os.path.join(os.path.expanduser('~'),'Desktop','Test_folder','Output','*.txt'))]    
filenames[0].extend(filenames[1])
filenames=filenames[0]

if( not os.path.isdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output'))):
    os.mkdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output'))
for fname in filenames:
    with open(fname) as file:
        for line in file.readlines():
            f = open(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output','{:}.txt'.format(os.path.split(fname)[-1] )), 'a+')
            f.write(line)
            f.close()    #This should take care of the permissions issue

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174748

As I understand it, you have multiple files in multiple directories. Some of these files have the same name, and you want to combine those files with the same name as one file, in another directory.

The first task, is to figure out what are all the unique file names in the directories; and collect the paths to those unique files.

import os, glob
from collections import defaultdict

dirs = [r'/foo/bar/directory_1', r'/foo/bar/directory_2']
file_pattrn = r'*.txt'
unique_files = defaultdict(list)

for d in dirs:
  for i in glob.iglob(os.path.join(d, file_pattrn)):
     unique_files[os.path.basename(i)].append(i)

Now we have a dictionary, where the key is the filename, and the value is a list of all the paths to that file.

Once we have that, then we just loop through them, to create our new files:

destination = r'/foo/bar/new_directory'
for unique_filename, copies in unique_files.items():
   with open(os.path.join(destination, unique_filename), 'w') as f:
     for copy in copies:
        with open(copy, 'r') as cp:
           for line in cp:
              f.write(line)

Upvotes: 0

Related Questions