read multi files from a folder and creat a new one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xiaoboren
    New Member
    • Jul 2007
    • 4

    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by xiaoboren
    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
    [code=Python]fn = 'your_output_fi le'
    outputfile = open(fn, 'w')
    for f in dirEntries('you r path', 0, 'txt'):
    outputfile.writ e('%s\n' % open(f).read())
    outputfile.clos e()[/code]Much easier than it sounds!

    Comment

    • xiaoboren
      New Member
      • Jul 2007
      • 4

      #3
      [CODE=python]import os

      def dirEntries(dir_ name, subdir, *args):
      '''Return a list of file names found in directory 'dir_name
      If 'subdir' is True, recursively access subdirectories under 'dir_name'.
      Additional arguments, if any, are file extensions to match filenames.
      Matched file names are added to the list.
      If there are no additional arguments, all files found in the directory are
      added to the list.
      Example usage: fileList = dir_list(r'H:\T EMP', False, 'txt', 'py')
      Only files with 'txt' and 'py' extensions will be added to the list.
      Example usage: fileList = dir_list(r'H:\T EMP', True)
      All files and all the files in subdirectories under H:\TEMP will be added
      to the list.
      '''
      fileList = []
      for file in os.listdir(dir_ name):
      dirfile = os.path.join(di r_name, file)
      if os.path.isfile( dirfile):
      if len(args) == 0:
      fileList.append (dirfile)
      else:
      if os.path.splitex t(dirfile)[1][1:] in args:
      fileList.append (dirfile)
      # recursively access file names in subdirectories
      elif os.path.isdir(d irfile) and subdir:
      print "Accessing directory:", dirfile
      fileList += dirEntries(dirf ile, subdir, *args)
      return fileList


      fn='out_putfile s'

      outputfile= open(fn,'w')
      for f in dirEntries( 'C:/Documents and Settings/ren/Desktop/JY',0,'txt'):
      outputfile.writ e('%s\n' % open(f).read())
      outputfile.clos e()

      # I changed the .txt to txt, but it still doesn't work. Here is the wrong message:
      [/CODE]Traceback (most recent call last):
      File "C:\Documen ts and Settings\ren\De sktop\JY\JY_fil es.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!
      Last edited by bartonc; Jul 23 '07, 05:15 PM. Reason: Added [CODE=python][/CODE] tags.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        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

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by xiaoboren
          [CODE=python]import os

          def dirEntries(dir_ name, subdir, *args):
          '''Return a list of file names found in directory 'dir_name
          If 'subdir' is True, recursively access subdirectories under 'dir_name'.
          Additional arguments, if any, are file extensions to match filenames.
          Matched file names are added to the list.
          If there are no additional arguments, all files found in the directory are
          added to the list.
          Example usage: fileList = dir_list(r'H:\T EMP', False, 'txt', 'py')
          Only files with 'txt' and 'py' extensions will be added to the list.
          Example usage: fileList = dir_list(r'H:\T EMP', True)
          All files and all the files in subdirectories under H:\TEMP will be added
          to the list.
          '''
          fileList = []
          for file in os.listdir(dir_ name):
          dirfile = os.path.join(di r_name, file)
          if os.path.isfile( dirfile):
          if len(args) == 0:
          fileList.append (dirfile)
          else:
          if os.path.splitex t(dirfile)[1][1:] in args:
          fileList.append (dirfile)
          # recursively access file names in subdirectories
          elif os.path.isdir(d irfile) and subdir:
          print "Accessing directory:", dirfile
          fileList += dirEntries(dirf ile, subdir, *args)
          return fileList


          fn='out_putfile s'

          outputfile= open(fn,'w')
          for f in dirEntries( 'C:/Documents and Settings/ren/Desktop/JY',0,'txt'):
          outputfile.writ e('%s\n' % open(f).read())
          outputfile.clos e()

          # I changed the .txt to txt, but it still doesn't work. Here is the wrong message:
          [/CODE]Traceback (most recent call last):
          File "C:\Documen ts and Settings\ren\De sktop\JY\JY_fil es.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:[code=Python]import os

          def dirEntries(dir_ name, subdir, *args):
          '''Return a list of file names found in directory 'dir_name'
          If 'subdir' is True, recursively access subdirectories under 'dir_name'.
          Additional arguments, if any, are file extensions to match filenames. Matched
          file names are added to the list.
          If there are no additional arguments, all files found in the directory are
          added to the list.
          Example usage: fileList = dir_list(r'H:\T EMP', False, 'txt', 'py')
          Only files with 'txt' and 'py' extensions will be added to the list.
          Example usage: fileList = dir_list(r'H:\T EMP', True)
          All files and all the files in subdirectories under H:\TEMP will be added
          to the list.
          '''
          fileList = []
          for file in os.listdir(dir_ name):
          dirfile = os.path.join(di r_name, file)
          if os.path.isfile( dirfile):
          if len(args) == 0:
          fileList.append (dirfile)
          else:
          if os.path.splitex t(dirfile)[1][1:] in args:
          fileList.append (dirfile)
          # recursively access file names in subdirectories
          elif os.path.isdir(d irfile) and subdir:
          print "Accessing directory:", dirfile
          [fileList.append (f) for f in dirEntries(dirf ile, subdir, *args)]
          return fileList[/code]

          Comment

          • xiaoboren
            New Member
            • Jul 2007
            • 4

            #6
            thank you very much, the problem is fixed. Thanks!
            You are expert.
            Thanks for your function.
            Thanks!

            Xiaobo

            Comment

            Working...