coping directories
Gabriel Genellina
gagsl-py at yahoo.com.ar
Fri Feb 2 11:33:19 EST 2007
"Gigs_" <gigs at hi.t-com.hr> escribió en el mensaje
news:epve95$ksd$1 at ss408.t-com.hr...
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.
>
> 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
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(todir, 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.BAT") with
os.path.join("c:\\temp", "\\AUTOEXEC.BAT") with os.path.join("c:\\temp\\",
"AUTOEXEC.BAT") with os.path.join("c:\\temp\\", "\\AUTOEXEC.BAT") - remember
that len("\\") == 1
- compare c:\temp c:\temp\AUTOEXEC.BAT and see where the filename part
begins and what your program is doing with this.
--
http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list