1

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

import os
import glob   

filenames = [glob.glob(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' ,'Desktop','Test_folder','Input','*.txt')), glob.glob(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.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop' ,'Test_folder', 'Test_output','{:}.txt'.format(os.path.split(fname)[-1] )), 'a+')
        f.write(line)
        f.close()    #This should take care of the permissions issue

But Getting Error:

os.mkdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output'))
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\HOME\\Desktop\\Test_output'
>>> 

Edited Code

import os
import glob  

filenames = [glob.glob(os.path.join('C:/Users/Vishnu/Desktop/Test_folder/Input/','*.txt')), glob.glob(os.path.join('C:/Users/Vishnu/Desktop/Test_folder/Output/','*.txt'))]    

for fname in filenames:
    with open(fname).readlines() as all_lines:
        for line in all_lines:
            f = open(r'C:/Users/Vishnu/Desktop/Test_output/{:}'.format(str(fname.split('/')[-1]), 'a')   
            f.write('{:}\n'.format(line)
            f.close()    

Error:

f.write('{:}\n'.format(line)
^
SyntaxError: invalid syntax
12
  • It pretty much says that the directory "Desktop" does not exist. Commented Oct 22, 2016 at 6:51
  • @table, But I have all file in the respective folder on "Desktop". I am not understand what are you trying to say? Commented Oct 22, 2016 at 6:57
  • C:\\HOME does not exist... Why are you expanding the tilde if you enter the full path? Commented Oct 22, 2016 at 6:59
  • Are you sure, that the C:\Home\Desktop is what you are going for? Cause in the code you are trying to use directory 'C:\Users\Vishnu\Desktop` Commented Oct 22, 2016 at 6:59
  • @table, My Input dir: C:\Users\Vishnu\Desktop\Test_folder\Input and C:\Users\Vishnu\Desktop\Test_folder\Output . Final output: C:\Users\Vishnu\Desktop\Test_folder\Test_output Commented Oct 22, 2016 at 7:06

3 Answers 3

4

You can use os.makedirs() to make sure that every directory along your path will be created(if it's not exist already).

Sign up to request clarification or add additional context in comments.

Comments

1

Some part of your path doesn't exist. You're using C:\HOME which is not the same as C:\User\HOME.

os.path.expanduser says the following in the documentation.

On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.

So it seems that only the username is being expanded, not the entire user home path.

Comments

0

I have to get this right, you explanation is not clear to me - I know it must be difficult if English is not your first language. This is the way I see it, please let me know if I am describing your problem correctly.

You have several folders, I'm going to call them folder1 and folder2. Within those folders you have files with the same name, for example folder1\file1.txt and folder2\file1.txt. These are to be concatenated (joined) into a third folder, folder3\file1.txt.

import glob
import os.path

# Alter these to match your folder names
folder1 = 'folder1'
folder2 = 'folder2'
folder3 = 'folder3'

if not os.path.isdir(folder3): 
    os.mkdir(folder3)
    print(folder3, "created")

for fname1 in glob.iglob(os.path.join(folder1, '*.txt')):
    basen = os.path.basename(fname1)
    fname2 = os.path.join(folder2, basen)
    if os.path.isfile(fname2):
        outfname = os.path.join(folder3, basen)
        with open(outfname, 'wb') as outf: 
            with open(fname1, 'rb') as f1:
                outf.write(f1.read())
            with open(fname2, 'rb') as f2:
                outf.write(f2.read())

Please let me know if this is NOT what you need.

Comments

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.