Python Script for Running a Python Program over Different Files inthe Directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shalen chhabra

    Python Script for Running a Python Program over Different Files inthe Directory

    Hey,

    Can anyone give me a snippet for running a python program over all the files
    in the directory.
    For ex: I have ten files in a directory and I want to run a python program
    against all of these files, I wish to do the same using another python code
    instead of running each of these files one by one, which would be cumbersome
    giving the argv of each file every single time.

    This can be easily done using a shell script but I just wanted to have a
    flavour of python for this.

    Thanks
    Shalen

    _______________ _______________ _______________ _______________ _____
    Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
    (Limited-time Offer) http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/


  • Brett Kelly

    #2
    Re: Python Script for Running a Python Program over Different Files in the Directory

    something like:

    import os

    for f in os.listdir('/path/to/dir'):
    <do stuff to f>

    that's the best way i know for doing something to each file in a directory...

    On Sat, 13 Mar 2004 08:22:09 +0000, Shalen chhabra wrote:
    [color=blue]
    > Hey,
    >
    > Can anyone give me a snippet for running a python program over all the files
    > in the directory.
    > For ex: I have ten files in a directory and I want to run a python program
    > against all of these files, I wish to do the same using another python code
    > instead of running each of these files one by one, which would be cumbersome
    > giving the argv of each file every single time.
    >
    > This can be easily done using a shell script but I just wanted to have a
    > flavour of python for this.
    >
    > Thanks
    > Shalen
    >
    > _______________ _______________ _______________ _______________ _____
    > Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
    > (Limited-time Offer) http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/[/color]

    Comment

    • Ivo

      #3
      Re: Python Script for Running a Python Program over Different Files inthe Directory

      import glob,os
      filelist=glob.g lob('/folder here/*') # retrieve your listing.
      for file in filelist:
      if os.path.isfile( file):
      <do your stuff here>
      --
      Cheerz,
      Ivo.



      =============== =============
      "Shalen chhabra" <shalen_itbhu@h otmail.com> wrote in message
      news:mailman.35 1.1079166133.19 534.python-list@python.org ...[color=blue]
      > Hey,
      >
      > Can anyone give me a snippet for running a python program over all the[/color]
      files[color=blue]
      > in the directory.
      > For ex: I have ten files in a directory and I want to run a python[/color]
      program[color=blue]
      > against all of these files, I wish to do the same using another python[/color]
      code[color=blue]
      > instead of running each of these files one by one, which would be[/color]
      cumbersome[color=blue]
      > giving the argv of each file every single time.
      >
      > This can be easily done using a shell script but I just wanted to have a
      > flavour of python for this.
      >
      > Thanks
      > Shalen
      >
      > _______________ _______________ _______________ _______________ _____
      > Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
      > (Limited-time Offer)[/color]
      http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/[color=blue]
      >
      >[/color]


      Comment

      • Andrei

        #4
        Re: Python Script for Running a Python Program over Different Files in the Directory

        Shalen chhabra wrote on Sat, 13 Mar 2004 08:22:09 +0000:
        [color=blue]
        > Can anyone give me a snippet for running a python program over all the files
        > in the directory.[/color]

        You can use os.listdir() to get all files in a given path as a list. Then
        you can loop over that list and do whatever you like to each item in that
        list.

        --
        Yours,

        Andrei

        =====
        Real contact info (decode with rot13):
        cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
        gur yvfg, fb gurer'f ab arrq gb PP.

        Comment

        • John Roth

          #5
          Re: Python Script for Running a Python Program over Different Files inthe Directory


          "Shalen chhabra" <shalen_itbhu@h otmail.com> wrote in message
          news:mailman.35 1.1079166133.19 534.python-list@python.org ...[color=blue]
          > Hey,
          >
          > Can anyone give me a snippet for running a python program over all the[/color]
          files[color=blue]
          > in the directory.
          > For ex: I have ten files in a directory and I want to run a python[/color]
          program[color=blue]
          > against all of these files, I wish to do the same using another python[/color]
          code[color=blue]
          > instead of running each of these files one by one, which would be[/color]
          cumbersome[color=blue]
          > giving the argv of each file every single time.
          >
          > This can be easily done using a shell script but I just wanted to have a
          > flavour of python for this.[/color]

          Given your reply to the attempts to help, I'm going to
          assume that what you want is to separate the actual manipulation
          of each file from the logic of determining which files to manipulate.

          If this isn't what you want, please stop reading now and don't
          bother to reply - it'll save both of us aggrivation.

          The answer to the problem is the visitor pattern. It's a
          standard pattern (see "Design Patterns" [GOF].)

          The directory program is:

          ---------- DirBase.py ------------------------
          # Basic classes for file maintenance

          import os, stat, os.path

          class DirectoryList(o bject):
          def __init__(self, pathName):
          self.dirList = os.listdir(path Name)
          self.pathName = pathName
          self.dirList.so rt()

          def walk(self, visitor):
          for fileName in self.dirList:
          filePath = os.path.join(se lf.pathName, fileName)
          fileStatus = os.stat(filePat h)
          if stat.S_ISREG(fi leStatus.st_mod e):
          visitor.doFile( filePath, fileStatus)
          else:
          visitor.doDir(f ilePath, fileStatus)

          class cleanDirectory( object):
          def doFile(self, filePath, fileStatus):
          os.remove(fileP ath)

          def doDir(self, dirPath, dirStatus):
          DirectoryList(d irPath).walk(cl eanDirectory())
          os.rmdir(dirPat h)

          def fetchFile(inDir Path, fileName):
          filePath = os.path.join(in DirPath, fileName)
          fileObj = open(filePath, 'rb')
          fileText = fileObj.read()
          fileObj.close()
          return fileText

          def fetchTextFile(i nDirPath, fileName):
          filePath = os.path.join(in DirPath, fileName)
          fileObj = open(filePath, 'rt')
          fileList = fileObj.readlin es()
          fileObj.close()
          return fileList

          def storeFile(fileT ext, outNameList, fileStatus):
          outFilePath = os.path.join(*o utNameList)
          outFileObj = open(outFilePat h, 'wb')
          outFileObj.writ e(fileText)
          outFileObj.clos e()
          os.utime(outFil ePath,(fileStat us.st_atime, fileStatus.st_m time))

          def storeTextFile(f ileList, outNameList, fileStatus):
          outFilePath = os.path.join(*o utNameList)
          outFileObj = open(outFilePat h, 'wt')
          outFileObj.writ elines(fileList )
          outFileObj.clos e()
          os.utime(outFil ePath,(fileStat us.st_atime, fileStatus.st_m time))

          ------------------------------------------------------------

          An example of how to use it is:

          ----------- MyFileManipulat ionProgram.py ---------------

          # reorganize files captured from the *** web site

          import os, stat, os.path
          import re
          from DirBase import *

          def setUpOutdir():
          DirectoryList(" outDir").walk(c leanDirectory() )

          class copyPicture(obj ect):
          def doFile(self, filePath, fileStatus):
          head, tail = os.path.split(f ilePath)
          fileText = fetchFile(head, tail)
          storeFile(fileT ext, ("outDir", "pics", tail), fileStatus)

          def doDir(self, dirPath, dirStatus):
          pass

          # precompile patterns used for multiple files
          script = re.compile(r"<s cript>.*?</script>")
          meta = re.compile(r"<M ETA.*?>")
          cmnt = re.compile(r"<\ !--.*?-->")
          cmnt1 = re.compile(r"<\ !--//-->")
          html = re.compile(r"\. html")

          class copyWebPage(obj ect):
          def doFile(self, inFilePath, fileStatus):
          head, tail = os.path.split(i nFilePath)
          fileName, extension = os.path.splitex t(tail)
          inFileText = fetchFile(head, tail)
          print ("path: '%s' head: '%s' tail: '%s' name: '%s' ext: '%s'\n" %
          (inFilePath, head, tail, fileName, extension))
          if extension == ".htm":
          inFileText = script.sub("", inFileText)
          #inFileText = meta.sub("", inFileText)
          inFileText = cmnt1.sub("", inFileText)
          inFileText = html.sub(".htm" , inFileText)
          subPattern = "%s_files" % tail[:-4]
          inFileText = re.sub(subPatte rn, "pics", inFileText)

          storeFile(inFil eText, ("outDir", tail), fileStatus)

          def doDir(self, dirPath, dirStatus):
          pass

          def main(inDirPath) :
          DirectoryList(i nDirPath).walk( copyWebPage())

          if __name__ == "__main__":
          setUpOutdir()
          main(r"c:\mydir ectory")

          ----------------------------------------------------------------------

          I have any number of file fixup programs that use the
          same DirBase.py program.

          HTH

          John Roth
          [color=blue]
          > Thanks
          > Shalen[/color]


          Comment

          • Ivo

            #6
            Re: Python Script for Running a Python Program over Different Files inthe Directory

            look at the execfile( filename[, globals[, locals]])

            command in de manpages

            or the following
            execl( path, arg0, arg1, ...)

            execle( path, arg0, arg1, ..., env)

            execlp( file, arg0, arg1, ...)

            execlpe( file, arg0, arg1, ..., env)

            execv( path, args)

            execve( path, args, env)

            execvp( file, args)

            execvpe( file, args, env)


            Use the other examples to iterate
            --
            Cheerz,
            Ivo.



            =============== =============
            "Shalen chhabra" <shalen_itbhu@h otmail.com> wrote in message
            news:mailman.35 1.1079166133.19 534.python-list@python.org ...[color=blue]
            > Hey,
            >
            > Can anyone give me a snippet for running a python program over all the[/color]
            files[color=blue]
            > in the directory.
            > For ex: I have ten files in a directory and I want to run a python[/color]
            program[color=blue]
            > against all of these files, I wish to do the same using another python[/color]
            code[color=blue]
            > instead of running each of these files one by one, which would be[/color]
            cumbersome[color=blue]
            > giving the argv of each file every single time.
            >
            > This can be easily done using a shell script but I just wanted to have a
            > flavour of python for this.
            >
            > Thanks
            > Shalen
            >
            > _______________ _______________ _______________ _______________ _____
            > Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
            > (Limited-time Offer)[/color]
            http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/[color=blue]
            >
            >[/color]


            Comment

            Working...