By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

merging files, each file in a new line

P: 9
I have this code to merge files. the files language is persian. it merges them but I want each file to be written in a new line. for example if I have 9 files, the output should be 9 lines. each line is one file. the problem is that I don't know where to put replace('\n', " ") to omit newlines of a file and where to add '\n' to go to the next file.

Expand|Select|Wrap|Line Numbers
  1. import os,shutil
  2. path = 'فناوري'
  3. f=open("wwww.txt","a", encoding = 'utf-8')
  4. for r,d,fi in os.walk(path):
  5.     for files in fi:
  6.         if files.endswith(".txt"):                         
  7.             g=open(os.path.join(r,files), encoding = 'utf-8')
  8.             shutil.copyfileobj(g,f)
  9.             g.close()
  10. f.close()
Mar 11 '15 #1
Share this Question
Share on Google+
8 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Given a list of filenames, create a string from the contents of each file. You can then write the string to your output file.
Expand|Select|Wrap|Line Numbers
  1. >>> files = ["text1.txt", "text2.txt"]
  2. >>> output = "\n".join([",".join([line.strip() for line in open(fn).readlines()]) for fn in files])
  3. >>> output
  4. 'abcdef,xyz\n123456,1098'
  5. >>>
Mar 11 '15 #2

P: 9
I added 2 lines to the code. it was working some hours ago, but now it doesn't work. I don't know the problem!!!!!!!!!!!can anybody help?
Expand|Select|Wrap|Line Numbers
  1. import os,shutil
  2. path = 'فناوري'
  3. f=open("wwww.txt","a", encoding = 'utf-8')
  4. for r,d,fi in os.walk(path):
  5.     for files in fi:
  6.         if files.endswith(".txt"):
  7.              g=open(os.path.join(r,files), encoding = 'utf-8')
  8.              for line in g:
  9.                  f.write(line.strip('\n') + ' ')
  10.                  shutil.copyfileobj(g,f)
  11.                  f.write('\n')
  12.                  g.close()
  13. f.close()
Mar 11 '15 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Please provide the error traceback or describe the issue.
Mar 11 '15 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Just noticed something. Eliminate the for loop on the file object g. shutil.copyfileobj() copies all of the file contents, not just the current line. Something like this:
Expand|Select|Wrap|Line Numbers
  1.         if files.endswith(".txt"):
  2.             g=open(os.path.join(r,files)) #, encoding='utf-8')
  3.             shutil.copyfileobj(g,f)
  4.             f.write('\n')
  5.             g.close()
Mar 11 '15 #5

P: 9
I want each file (which has some lines) to be written in just one line. so I need to omit newlines between them. and the to write the next file as the next line I need to add '\n'. so I cant eliminate for loop. If I eliminate it of course it merges them but there are a lot of lines. each file should be = one line
Mar 11 '15 #6

bvdet
Expert Mod 2.5K+
P: 2,851
Did you try the code in my first reply? That's the functionality you seek, or is it not? It uses a comma delimiter for lines in a file, but you can change that to whatever character you want.
Mar 11 '15 #7

P: 9
I started python recently. sorry for so much questions. open (fn)reads a file but I have a folder which contains 10 files. what should I write instead of fn so that it reads the folder. I know I have to split the folder first with os.listdir (folder) but then what?
Mar 11 '15 #8

bvdet
Expert Mod 2.5K+
P: 2,851
Create the list of file names first. Then iterate on the list of file names.
Expand|Select|Wrap|Line Numbers
  1. import os
  2. path = r"X:\temp\macro\test"
  3. files = os.listdir(path)
  4. outputList = []
  5. for fn in files:
  6.     if fn.endswith("txt"):
  7.         outputList.append(",".join([line.strip() for line in open(os.path.join(path, fn)).readlines()]))
  8. output = "\n".join(outputList)
  9. print output
Mar 11 '15 #9

Post your reply

Sign in to post your reply or Sign up for a free account.