combined files together

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    combined files together

    Hi

    is there a module to do things like concatenate all files in a given
    directory into a big file, where all the files have the same data
    formate?
    name address phone_no.

    or do I have to open each, read from old/write-or-append to new ...

    thanks

  • Gary Herron

    #2
    Re: combined files together

    Gary Wessle wrote:
    [color=blue]
    >Hi
    >
    >is there a module to do things like concatenate all files in a given
    >directory into a big file, where all the files have the same data
    >formate?
    >name address phone_no.
    >
    >or do I have to open each, read from old/write-or-append to new ...
    >
    >thanks
    >
    >
    >[/color]
    There's hardly enough code here to make a module out of this:

    combined = open('...', 'wb')
    for name in os.listdir(path ):
    infile = open(os.path.jo in(path,name), 'rb')
    for line in infile:
    combined.write( line)

    It could be more efficient by reading larger chunks than single lines,
    and could be more accurate by closing both input and output files when
    done, but you get the point I hope.

    On the other hand, if you've got the right OS, you might try something like:
    os.system("cat * > combined")

    Gary Herron



    Comment

    • Gary Wessle

      #3
      Re: combined files together

      Gary Herron <gherron@island training.com> writes:
      [color=blue]
      > Gary Wessle wrote:
      >[color=green]
      > >Hi
      > >
      > >is there a module to do things like concatenate all files in a given
      > >directory into a big file, where all the files have the same data
      > >formate?
      > >name address phone_no.
      > >
      > >or do I have to open each, read from old/write-or-append to new ...
      > >
      > >thanks
      > >
      > >[/color]
      > There's hardly enough code here to make a module out of this:
      >
      > combined = open('...', 'wb')
      > for name in os.listdir(path ):[/color]

      I need to traverse those files in the order they were created
      chronologically . listdir() does not do it, is there a way besides
      build a list then list.sort(), then for element in list_of_files open
      element?

      thanks
      [color=blue]
      > infile = open(os.path.jo in(path,name), 'rb')
      > for line in infile:
      > combined.write( line)
      >
      > It could be more efficient by reading larger chunks than single lines,
      > and could be more accurate by closing both input and output files when
      > done, but you get the point I hope.
      >
      > On the other hand, if you've got the right OS, you might try something like:
      > os.system("cat * > combined")
      >
      > Gary Herron[/color]

      Comment

      • Eric Deveaud

        #4
        Re: combined files together

        Gary Wessle wrote:[color=blue]
        >
        > I need to traverse those files in the order they were created
        > chronologically . listdir() does not do it, is there a way besides
        > build a list then list.sort(), then for element in list_of_files open
        > element?[/color]

        are the name of the files describing the cration date, or have to rely on the
        creation date ?

        if the name allows to discriminate the chronology, check glob module.

        Eric

        Comment

        • Gary Wessle

          #5
          Re: combined files together

          Eric Deveaud <edeveaud@paste ur.fr> writes:
          [color=blue]
          > Gary Wessle wrote:[color=green]
          > >
          > > I need to traverse those files in the order they were created
          > > chronologically . listdir() does not do it, is there a way besides
          > > build a list then list.sort(), then for element in list_of_files open
          > > element?[/color]
          >
          > are the name of the files describing the cration date,[/color]

          yes
          [color=blue]
          > or have to rely on the creation date ?[/color]

          no

          [color=blue]
          >
          > if the name allows to discriminate the chronology, check glob module.[/color]

          I just tried glob, it does not put out a list with file names sorted.[color=blue]
          >
          > Eric[/color]

          Comment

          Working...