471,348 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

read multi files from a folder and creat a new one

Hi,
Thank you for read my problem.

My problem:

I have a folder which contains some .txt files, I want to read all of these files and write the data of these files into a new file.

Thanks for your help advance!!
I use Python 2.5 in Windows platform.
Jul 20 '07 #1
5 3235
bvdet
2,851 Expert Mod 2GB
Hi,
Thank you for read my problem.

My problem:

I have a folder which contains some .txt files, I want to read all of these files and write the data of these files into a new file.

Thanks for your help advance!!
I use Python 2.5 in Windows platform.
I will use the function I posted in this thread: LINK
Expand|Select|Wrap|Line Numbers
  1. fn = 'your_output_file'
  2. outputfile = open(fn, 'w')
  3. for f in dirEntries('your path', 0, 'txt'):
  4.     outputfile.write('%s\n' % open(f).read())
  5. outputfile.close()
Much easier than it sounds!
Jul 20 '07 #2
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def dirEntries(dir_name, subdir, *args):
  4.     '''Return a list of file names found in directory 'dir_name
  5.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  6.     Additional arguments, if any, are file extensions to match filenames.
  7.     Matched file names are added to the list.
  8.     If there are no additional arguments, all files found in the directory are
  9.     added to the list.
  10.     Example usage: fileList = dir_list(r'H:\TEMP', False, 'txt', 'py')
  11.     Only files with 'txt' and 'py' extensions will be added to the list.
  12.     Example usage: fileList = dir_list(r'H:\TEMP', True)
  13.     All files and all the files in subdirectories under H:\TEMP will be added
  14.     to the list.
  15.      '''
  16.     fileList = []
  17.     for file in os.listdir(dir_name):
  18.         dirfile = os.path.join(dir_name, file)
  19.         if os.path.isfile(dirfile):
  20.             if len(args) == 0:
  21.                 fileList.append(dirfile)
  22.             else:
  23.                 if os.path.splitext(dirfile)[1][1:] in args:
  24.                     fileList.append(dirfile)
  25.               # recursively access file names in subdirectories
  26.                 elif os.path.isdir(dirfile) and subdir:
  27.                     print "Accessing directory:", dirfile
  28.                     fileList += dirEntries(dirfile, subdir, *args)
  29.                     return fileList
  30.  
  31.  
  32. fn='out_putfiles'
  33.  
  34. outputfile= open(fn,'w')
  35. for f in dirEntries( 'C:/Documents and Settings/ren/Desktop/JY',0,'txt'):   
  36.     outputfile.write('%s\n' % open(f).read())
  37. outputfile.close()
  38.  
  39. # I changed the .txt  to txt, but it still doesn't work. Here is the wrong message:
  40.  
Traceback (most recent call last):
File "C:\Documents and Settings\ren\Desktop\JY\JY_files.py", line 34, in <module>
for f in dirEntries( 'C:/Documents and Settings/ren/Desktop/JY',0,'txt'):
TypeError: 'NoneType' object is not iterable#

I don't know why!!!
Thanks!
Jul 23 '07 #3
bartonc
6,596 Expert 4TB
Hi xiaoboren. I've added the CODE tags to your post. Instructions for doing this yourself are on the right hand side of the page when you reply. They are called "REPLY GUIDELINES". Thank you for taking the time to read them.

Welcome,
Barton
Jul 23 '07 #4
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def dirEntries(dir_name, subdir, *args):
  4.     '''Return a list of file names found in directory 'dir_name
  5.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  6.     Additional arguments, if any, are file extensions to match filenames.
  7.     Matched file names are added to the list.
  8.     If there are no additional arguments, all files found in the directory are
  9.     added to the list.
  10.     Example usage: fileList = dir_list(r'H:\TEMP', False, 'txt', 'py')
  11.     Only files with 'txt' and 'py' extensions will be added to the list.
  12.     Example usage: fileList = dir_list(r'H:\TEMP', True)
  13.     All files and all the files in subdirectories under H:\TEMP will be added
  14.     to the list.
  15.      '''
  16.     fileList = []
  17.     for file in os.listdir(dir_name):
  18.         dirfile = os.path.join(dir_name, file)
  19.         if os.path.isfile(dirfile):
  20.             if len(args) == 0:
  21.                 fileList.append(dirfile)
  22.             else:
  23.                 if os.path.splitext(dirfile)[1][1:] in args:
  24.                     fileList.append(dirfile)
  25.               # recursively access file names in subdirectories
  26.                 elif os.path.isdir(dirfile) and subdir:
  27.                     print "Accessing directory:", dirfile
  28.                     fileList += dirEntries(dirfile, subdir, *args)
  29.                     return fileList
  30.  
  31.  
  32. fn='out_putfiles'
  33.  
  34. outputfile= open(fn,'w')
  35. for f in dirEntries( 'C:/Documents and Settings/ren/Desktop/JY',0,'txt'):   
  36.     outputfile.write('%s\n' % open(f).read())
  37. outputfile.close()
  38.  
  39. # I changed the .txt  to txt, but it still doesn't work. Here is the wrong message:
  40.  
Traceback (most recent call last):
File "C:\Documents and Settings\ren\Desktop\JY\JY_files.py", line 34, in <module>
for f in dirEntries( 'C:/Documents and Settings/ren/Desktop/JY',0,'txt'):
TypeError: 'NoneType' object is not iterable#

I don't know why!!!
Thanks!
Your indentation is wrong in the function dirEntries(). It should look like this:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def dirEntries(dir_name, subdir, *args):
  4.     '''Return a list of file names found in directory 'dir_name'
  5.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  6.     Additional arguments, if any, are file extensions to match filenames. Matched
  7.         file names are added to the list.
  8.     If there are no additional arguments, all files found in the directory are
  9.         added to the list.
  10.     Example usage: fileList = dir_list(r'H:\TEMP', False, 'txt', 'py')
  11.         Only files with 'txt' and 'py' extensions will be added to the list.
  12.     Example usage: fileList = dir_list(r'H:\TEMP', True)
  13.         All files and all the files in subdirectories under H:\TEMP will be added
  14.         to the list.
  15.     '''
  16.     fileList = []
  17.     for file in os.listdir(dir_name):
  18.         dirfile = os.path.join(dir_name, file)
  19.         if os.path.isfile(dirfile):
  20.             if len(args) == 0:
  21.                 fileList.append(dirfile)
  22.             else:
  23.                 if os.path.splitext(dirfile)[1][1:] in args:
  24.                     fileList.append(dirfile)
  25.         # recursively access file names in subdirectories
  26.         elif os.path.isdir(dirfile) and subdir:
  27.             print "Accessing directory:", dirfile
  28.             [fileList.append(f) for f in dirEntries(dirfile, subdir, *args)]
  29.     return fileList
Jul 23 '07 #5
thank you very much, the problem is fixed. Thanks!
You are expert.
Thanks for your function.
Thanks!

Xiaobo
Jul 23 '07 #6

Post your reply

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

Similar topics

3 posts views Thread by shyju_kambi | last post: by
7 posts views Thread by Ivan | last post: by
2 posts views Thread by AAJ | last post: by
7 posts views Thread by =?Utf-8?B?SmVmZkRvdE5ldA==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.