coping directories

G

Gigs_

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!!!
:eek:
 
G

Gabriel Genellina

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.
 
G

Gigs_

Gabriel said:
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
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
 
G

Gigs_

Gabriel said:
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 + 1c:\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 thisc:\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
 
J

Jussi Salmela

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!!!
:eek:

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\AUTOEXEC.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
 
G

Gigs_

Jussi said:
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!!!
:eek:

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\AUTOEXEC.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
 
G

Gabriel Genellina

"Gigs_" <[email protected]> escribió en el mensaje
Gabriel said:
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 + 1c:\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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top