473,241 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 3328
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

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

Similar topics

3
by: shyju_kambi | last post by:
hi all, please give me the logic to creat a folder lock in windows2000/Xp using c program. what i mean by folder lock is, if any one is trying to open that folder, it should ask for the password...
7
by: Ivan | last post by:
Hi All, i found that all the referenced DLL files should be in the same folder of the execute file what can i do if i want all referenced DLL files located in a different path to the execute...
5
by: Mitchell S. Honnert | last post by:
Is there a way, given the full path of a folder on a network, that one can programatically tell if you have Read access to that folder? I have an application where the user is able to select a...
0
by: Siew-Ming | last post by:
Hi, Try to figure a way to turn off a folder Read-only property for a specific folder (Windows 2003) at multi sites. When we do our application upgrade/patch, we manually turn off the...
2
by: AAJ | last post by:
Hi all I have a few questions about file locations with VS2005, localhost WidowsXP and remotehost Server2003, and I wonder if anyone can help me get started 1) If I build a web site on my...
7
by: =?Utf-8?B?SmVmZkRvdE5ldA==?= | last post by:
I have an asp.net application using a multi-page wizard control that grabs user selected files from a database and allows the user to configure parameters using controls on the wizard pages. The...
6
by: tedpottel | last post by:
Hi, I'm trying to create my own lib of functions, but it seems like I can only import them if they are in pythons lib folder. Example I have a folder called K:\mypython Now in the...
3
akashazad
by: akashazad | last post by:
Hi guys in my application (VB6.0). I want to creat a folder in to which i want to creat a text file . so is there any one who can give me code for that . ...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.