coping directories

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

    coping directories

    hi people

    I have problem with this example, not actually the problem, but
    Code:
    class FileVisitor(object):
    def __init__(self, data=None):
    self.context = data
    def run(self, startdir=os.curdir):
    os.path.walk(startdir, self.visitor, None)
    def visitor(self, data, dirname, filesindir):
    self.visitdir(dirname)
    for fname in filesindir:
    fpath = os.path.join(dirname, fname)
    if not os.path.isdir(fpath):
    self.visitfile(fpath)
    def visitdir(self, dirpath):            # override or extend this
    method
    print dirpath, '...'
    def visitfile(self, filepath):          # override or extend this
    method
    print self.fcount, '=>', filepath
    #
    class CVisitor(FileVisitor):
    def __init__(self, fromdir, todir):
    self.fromdirLen = len(fromdir) + 1        # here is my problem
    self.todir = todir
    FileVisitor.__init__(self, fromdir)
    def visitdir(self, dirpath):
    topath = os.path.join(self.todir, dirpath[self.fromdirLen:])
    os.mkdir(topath)
    def visitfile(self, filepath):
    topath = os.path.join(self.todir, filepath[self.fromdirLen:])
    cpfile(filepath, topath)    #copy contents from filepath to
    topath

    When I copy contents from C:\IronPython to C:\temp
    its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
    self.fromdirLen = len(fromdir) + 1
    but when I change self.fromdirLen = len(fromdir) + 1 to self.fromdirLen
    = len(fromdir) i get contents copied to C:\ (actually to parent dir)

    Can anyone explain me that?

    Thanks!!!
    :o
  • Gabriel Genellina

    #2
    Re: coping directories

    En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gigs@hi.t-com.hrescribió:
    class CVisitor(FileVi sitor):
    def __init__(self, fromdir, todir):
    self.fromdirLen = len(fromdir) + 1 # here is my problem
    self.todir = todir
    FileVisitor.__i nit__(self, fromdir)
    def visitdir(self, dirpath):
    topath = os.path.join(se lf.todir, dirpath[self.fromdirLen :])
    os.mkdir(topath )
    def visitfile(self, filepath):
    topath = os.path.join(se lf.todir, filepath[self.fromdirLen :])
    cpfile(filepath , topath) #copy contents from filepath to
    topath[/code]
    >
    >
    When I copy contents from C:\IronPython to C:\temp
    its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
    self.fromdirLen = len(fromdir) + 1
    but when I change self.fromdirLen = len(fromdir) + 1 to self.fromdirLen
    = len(fromdir) i get contents copied to C:\ (actually to parent dir)
    Instead of actually doing os.mkdir and cpfile, use a print statement to
    output the involved variables, and try with and without +1. You'll see
    yourself what happens.

    --
    Gabriel Genellina

    Comment

    • Gigs_

      #3
      Re: coping directories

      Gabriel Genellina wrote:
      En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gigs@hi.t-com.hrescribió:
      >
      >class CVisitor(FileVi sitor):
      > def __init__(self, fromdir, todir):
      > self.fromdirLen = len(fromdir) + 1 # here is my problem
      > self.todir = todir
      > FileVisitor.__i nit__(self, fromdir)
      > def visitdir(self, dirpath):
      > topath = os.path.join(se lf.todir, dirpath[self.fromdirLen :])
      > os.mkdir(topath )
      > def visitfile(self, filepath):
      > topath = os.path.join(se lf.todir, filepath[self.fromdirLen :])
      > cpfile(filepath , topath) #copy contents from filepath to
      >topath[/code]
      >>
      >>
      >When I copy contents from C:\IronPython to C:\temp
      >its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
      >self.fromdirLe n = len(fromdir) + 1
      >but when I change self.fromdirLen = len(fromdir) + 1 to self.fromdirLen
      >= len(fromdir) i get contents copied to C:\ (actually to parent dir)
      >
      Instead of actually doing os.mkdir and cpfile, use a print statement to
      output the involved variables, and try with and without +1. You'll see
      yourself what happens.
      >
      --Gabriel Genellina
      >
      but when I change
      self.fromdirLen = len(fromdir) + 1 to self.fromdirLen = len(fromdir) i
      get contents copied to C:\ (actually to parent dir) instead to C:\temp

      Comment

      • Gigs_

        #4
        Re: coping directories

        Gabriel Genellina wrote:
        En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gigs@hi.t-com.hrescribió:
        >
        >class CVisitor(FileVi sitor):
        > def __init__(self, fromdir, todir):
        > self.fromdirLen = len(fromdir) + 1 # here is my problem
        > self.todir = todir
        > FileVisitor.__i nit__(self, fromdir)
        > def visitdir(self, dirpath):
        > topath = os.path.join(se lf.todir, dirpath[self.fromdirLen :])
        > os.mkdir(topath )
        > def visitfile(self, filepath):
        > topath = os.path.join(se lf.todir, filepath[self.fromdirLen :])
        > cpfile(filepath , topath) #copy contents from filepath to
        >topath[/code]
        >>
        >>
        >When I copy contents from C:\IronPython to C:\temp
        >its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
        >self.fromdirLe n = len(fromdir) + 1
        >but when I change self.fromdirLen = len(fromdir) + 1 to self.fromdirLen
        >= len(fromdir) i get contents copied to C:\ (actually to parent dir)
        >
        Instead of actually doing os.mkdir and cpfile, use a print statement to
        output the involved variables, and try with and without +1. You'll see
        yourself what happens.
        >
        --Gabriel Genellina
        >
        well I have tried with print but can't figure out
        I got this when I have removed + 1
        >>C = CpallVisitor('C :\\New', 'c:\\temp')
        >>C.run(startdi r='C:\\New')
        c:\temp\
        filepath: C:\New\AUTOEXEC .BAT Topath: \AUTOEXEC.BAT
        filepath: C:\New\boot.ini Topath: \boot.ini
        filepath: C:\New\CONFIG.S YS Topath: \CONFIG.SYS

        but needs to be this
        >>C = CpallVisitor('C :\\New', 'c:\\temp')
        >>C.run(startdi r='C:\\New')
        c:\temp\
        filepath: C:\New\AUTOEXEC .BAT Topath: c:\temp\AUTOEXE C.BAT
        filepath: C:\New\boot.ini Topath: c:\temp\boot.in i
        filepath: C:\New\CONFIG.S YS Topath: c:\temp\CONFIG. SYS


        In python shell I got same thing, no matter fromdirLen is
        len(fromdir) + 1 or len(fromdir)
        >>fromdir = 'C:\\New'
        >>fromdirLen = len(fromdir)
        >>todir = 'C:\\temp'
        >>topath = os.path.join(to dir, fromdir[fromdirLen:])
        >>topath
        'C:\\temp\\'
        >>fromdirLen = len(fromdir)+1
        >>topath = os.path.join(to dir, fromdir[fromdirLen:])
        >>topath
        'C:\\temp\\'
        >>fromdir[fromdirLen:]
        ''
        >>fromdirLen = len(fromdir)
        >>fromdir[fromdirLen:]
        ''
        >>fromdir
        'C:\\New'


        Please help

        Comment

        • Jussi Salmela

          #5
          Re: coping directories

          Gigs_ kirjoitti:
          hi people
          >
          I have problem with this example, not actually the problem, but
          Code:
          class FileVisitor(object):
              def __init__(self, data=None):
                  self.context = data
              def run(self, startdir=os.curdir):
                  os.path.walk(startdir, self.visitor, None)
              def visitor(self, data, dirname, filesindir):
                  self.visitdir(dirname)
                  for fname in filesindir:
                      fpath = os.path.join(dirname, fname)
                      if not os.path.isdir(fpath):
                          self.visitfile(fpath)
              def visitdir(self, dirpath):            # override or extend this
          method
                  print dirpath, '...'
              def visitfile(self, filepath):          # override or extend this
          method
                  print self.fcount, '=>', filepath
          #
          class CVisitor(FileVisitor):
              def __init__(self, fromdir, todir):
                  self.fromdirLen = len(fromdir) + 1        # here is my problem
                  self.todir = todir
                  FileVisitor.__init__(self, fromdir)
              def visitdir(self, dirpath):
                  topath = os.path.join(self.todir, dirpath[self.fromdirLen:])
                  os.mkdir(topath)
              def visitfile(self, filepath):
                  topath = os.path.join(self.todir, filepath[self.fromdirLen:])
                  cpfile(filepath, topath)    #copy contents from filepath to
          topath
          >
          >
          When I copy contents from C:\IronPython to C:\temp
          its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
          self.fromdirLen = len(fromdir) + 1
          but when I change self.fromdirLen = len(fromdir) + 1 to self.fromdirLen
          = len(fromdir) i get contents copied to C:\ (actually to parent dir)
          >
          Can anyone explain me that?
          >
          Thanks!!!
          :o
          Why do you want to change a working program anyway? :)

          The result of your change is that os.path.join does the join
          differently. Before the change the join is, for example:
          os.path.join(r' c:\temp', r'AUTOEXEC.BAT' )
          with a result:
          c:\temp\AUTOEXE C.BAT

          After your change the join is:
          os.path.join(r' c:\temp', r'\AUTOEXEC.BAT ')
          with a result:
          \AUTOEXEC.BAT

          This is described in the doc:

          join( path1[, path2[, ...]])

          Join one or more path components intelligently. If any component is an
          absolute path, all previous components (on Windows, including the
          previous drive letter, if there was one) are thrown away, and joining
          continues.

          HTH,
          Jussi

          Comment

          • Gigs_

            #6
            Re: coping directories

            Jussi Salmela wrote:
            Gigs_ kirjoitti:
            >hi people
            >>
            >I have problem with this example, not actually the problem, but
            >
            Code:
            >class FileVisitor(object):
            >    def __init__(self, data=None):
            >        self.context = data
            >    def run(self, startdir=os.curdir):
            >        os.path.walk(startdir, self.visitor, None)
            >    def visitor(self, data, dirname, filesindir):
            >        self.visitdir(dirname)
            >        for fname in filesindir:
            >            fpath = os.path.join(dirname, fname)
            >            if not os.path.isdir(fpath):
            >                self.visitfile(fpath)
            >    def visitdir(self, dirpath):            # override or extend this
            >method
            >        print dirpath, '...'
            >    def visitfile(self, filepath):          # override or extend this
            >method
            >        print self.fcount, '=>', filepath
            >#
            >class CVisitor(FileVisitor):
            >    def __init__(self, fromdir, todir):
            >        self.fromdirLen = len(fromdir) + 1        # here is my problem
            >        self.todir = todir
            >        FileVisitor.__init__(self, fromdir)
            >    def visitdir(self, dirpath):
            >        topath = os.path.join(self.todir, dirpath[self.fromdirLen:])
            >        os.mkdir(topath)
            >    def visitfile(self, filepath):
            >        topath = os.path.join(self.todir, filepath[self.fromdirLen:])
            >        cpfile(filepath, topath)    #copy contents from filepath to
            >topath
            >>
            >>
            >When I copy contents from C:\IronPython to C:\temp
            >its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
            >self.fromdirLe n = len(fromdir) + 1
            >but when I change self.fromdirLen = len(fromdir) + 1 to
            >self.fromdirLe n = len(fromdir) i get contents copied to C:\ (actually
            >to parent dir)
            >>
            >Can anyone explain me that?
            >>
            >Thanks!!!
            > :o
            >
            Why do you want to change a working program anyway? :)
            >
            The result of your change is that os.path.join does the join
            differently. Before the change the join is, for example:
            os.path.join(r' c:\temp', r'AUTOEXEC.BAT' )
            with a result:
            c:\temp\AUTOEXE C.BAT
            >
            After your change the join is:
            os.path.join(r' c:\temp', r'\AUTOEXEC.BAT ')
            with a result:
            \AUTOEXEC.BAT
            >
            This is described in the doc:
            >
            join( path1[, path2[, ...]])
            >
            Join one or more path components intelligently. If any component is an
            absolute path, all previous components (on Windows, including the
            previous drive letter, if there was one) are thrown away, and joining
            continues.
            >
            HTH,
            Jussi
            thats what i need to know
            Thanks

            Comment

            • Gabriel Genellina

              #7
              Re: coping directories

              "Gigs_" <gigs@hi.t-com.hrescribió en el mensaje
              news:epve95$ksd $1@ss408.t-com.hr...
              Gabriel Genellina wrote:
              En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gigs@hi.t-com.hrescribió:
              >
              >class CVisitor(FileVi sitor):
              > def __init__(self, fromdir, todir):
              > self.fromdirLen = len(fromdir) + 1 # here is my problem
              > self.todir = todir
              > FileVisitor.__i nit__(self, fromdir)
              > def visitdir(self, dirpath):
              > topath = os.path.join(se lf.todir, dirpath[self.fromdirLen :])
              > os.mkdir(topath )
              > def visitfile(self, filepath):
              > topath = os.path.join(se lf.todir, filepath[self.fromdirLen :])
              > cpfile(filepath , topath) #copy contents from filepath to
              >topath[/code]
              >>
              >>
              >When I copy contents from C:\IronPython to C:\temp
              >its all goes fine when self.fromdirLen = len(fromdir) + 1 is like this
              >self.fromdirLe n = len(fromdir) + 1
              >but when I change self.fromdirLen = len(fromdir) + 1 to self.fromdirLen
              >= len(fromdir) i get contents copied to C:\ (actually to parent dir)
              >
              Instead of actually doing os.mkdir and cpfile, use a print statement to
              output the involved variables, and try with and without +1. You'll see
              yourself what happens.
              >
              well I have tried with print but can't figure out
              I got this when I have removed + 1
              >>C = CpallVisitor('C :\\New', 'c:\\temp')
              >>C.run(startdi r='C:\\New')
              c:\temp\
              filepath: C:\New\AUTOEXEC .BAT Topath: \AUTOEXEC.BAT
              filepath: C:\New\boot.ini Topath: \boot.ini
              filepath: C:\New\CONFIG.S YS Topath: \CONFIG.SYS
              So it's clear that you need the +1 for the program to work properly, ok?
              In python shell I got same thing, no matter fromdirLen is
              len(fromdir) + 1 or len(fromdir)
              >>fromdir = 'C:\\New'
              >>fromdirLen = len(fromdir)
              >>todir = 'C:\\temp'
              >>topath = os.path.join(to dir, fromdir[fromdirLen:])
              >>topath
              'C:\\temp\\'
              This is *not* what your program does; the original code above has another
              variable, filepath.
              Please help
              I assume that you're doing this as some kind of learning exercise - else
              there are other simpler ways. And you want to know why do you need that +1.
              Because it's clear that using +1 is the right answer, ok? You'll have to
              understand it yourself. Try running the program step by step. Hints:
              - compare os.path.join("c :\\temp", "AUTOEXEC.B AT") with
              os.path.join("c :\\temp", "\\AUTOEXEC.BAT ") with os.path.join("c :\\temp\\",
              "AUTOEXEC.B AT") with os.path.join("c :\\temp\\", "\\AUTOEXEC.BAT ") - remember
              that len("\\") == 1
              - compare c:\temp c:\temp\AUTOEXE C.BAT and see where the filename part
              begins and what your program is doing with this.

              --



              Comment

              Working...