Need help: Compiling Python-Code &In-Reply-To=

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CRAIG DALTON

    Need help: Compiling Python-Code &In-Reply-To=

    Hi,

    I'm looking to append several text files in one director and out put the combined files into another director. I'm new to Python and just can't get itto work. So far I've been able to create a file in the desired directory but it isn't pulling any of the data in the originating directory. Could please look at my code and tell me what I'm doing wrong.

    import os,shutil
    f=open("c:\\out put\\testing1.t xt","a+")
    for r,d,fi in os.walk("c:\\te st"):
    for files in fi:
    if files.endswith( ".txt"):
    g=open(os.path. join(r+,files))
    shutil.copyfile obj(g,f)
    g.close()
    f.close()

    Any help would be great.

    Thanks,

    Craig Dalton
    Business Applications Systems Analyst
    Sentara Healthcare Systems
    Information Technology
    cxdalton@sentar a.com


  • Larry Bates

    #2
    Re: Need help: Compiling Python-Code &In-Reply-To=

    CRAIG DALTON wrote:
    Hi,
    >
    I'm looking to append several text files in one director and out put the combined files into another director. I'm new to Python and just can't get it to work. So far I've been able to create a file in the desired directory but it isn't pulling any of the data in the originating directory. Could please look at my code and tell me what I'm doing wrong.
    >
    import os,shutil
    f=open("c:\\out put\\testing1.t xt","a+")
    for r,d,fi in os.walk("c:\\te st"):
    for files in fi:
    if files.endswith( ".txt"):
    g=open(os.path. join(r+,files))
    shutil.copyfile obj(g,f)
    g.close()
    f.close()
    >
    Any help would be great.
    >
    Thanks,
    >
    Craig Dalton
    Business Applications Systems Analyst
    Sentara Healthcare Systems
    Information Technology
    cxdalton@sentar a.com
    >
    >
    I don't think what you pasted will work at all. The r+ should produce a syntax
    error on the g=open(os.path. join(r+,files) line.

    No real reason to get shutil involved in this.

    try something like this (not tested):

    outfp=open("c:\ \output\\testin g1.txt", 'w')
    for root,dirs,files in os.walk("c:\\te st"):
    for fname in files:
    if fname.endswith( ".txt"):
    infp=open(os.pa th.join(root, fname), 'r')
    of.write(infp.r ead())
    infp.close()

    outfp.close()

    or if the files are binary:

    use 'wb' and 'rb' instead of 'w' and 'r'

    BTW - This will concatenate ALL the .TXT files in c:\test as well as any files
    in any subfolders into a single output file.

    -Larry

    Comment

    • Gabriel Genellina

      #3
      Re: Need help: Compiling Python-Code &In-Reply-To=

      En Fri, 02 May 2008 18:39:36 -0300, Larry Bates <larry.bates@we bsafe.com`>
      escribió:
      No real reason to get shutil involved in this.
      ....except memory usage; shutil.copyfile obj reads the input in chunks, but
      your version reads the whole file in memory. Depending on the file sizes,
      this might be an issue.

      --
      Gabriel Genellina

      Comment

      Working...