coping directories

Gigs_ gigs at hi.t-com.hr
Fri Feb 2 08:27:59 EST 2007


Gabriel Genellina wrote:
> En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gigs at hi.t-com.hr> escribió:
> 
>> 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[/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
> 
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(startdir='C:\\New')
c:\temp\
filepath: C:\New\AUTOEXEC.BAT     Topath: \AUTOEXEC.BAT
filepath: C:\New\boot.ini     Topath: \boot.ini
filepath: C:\New\CONFIG.SYS     Topath: \CONFIG.SYS

but needs to be this
 >>> C = CpallVisitor('C:\\New', 'c:\\temp')
 >>> C.run(startdir='C:\\New')
c:\temp\
filepath: C:\New\AUTOEXEC.BAT     Topath: c:\temp\AUTOEXEC.BAT
filepath: C:\New\boot.ini     Topath: c:\temp\boot.ini
filepath: C:\New\CONFIG.SYS     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(todir, fromdir[fromdirLen:])
 >>> topath
'C:\\temp\\'
 >>> fromdirLen = len(fromdir)+1
 >>> topath = os.path.join(todir, fromdir[fromdirLen:])
 >>> topath
'C:\\temp\\'
 >>> fromdir[fromdirLen:]
''
 >>> fromdirLen = len(fromdir)
 >>> fromdir[fromdirLen:]
''
 >>> fromdir
'C:\\New'


Please help



More information about the Python-list mailing list