473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

coping directories

hi people

I have problem with this example, not actually the problem, but
Expand|Select|Wrap|Line Numbers
  1. class FileVisitor(object):
  2. def __init__(self, data=None):
  3. self.context = data
  4. def run(self, startdir=os.curdir):
  5. os.path.walk(startdir, self.visitor, None)
  6. def visitor(self, data, dirname, filesindir):
  7. self.visitdir(dirname)
  8. for fname in filesindir:
  9. fpath = os.path.join(dirname, fname)
  10. if not os.path.isdir(fpath):
  11. self.visitfile(fpath)
  12. def visitdir(self, dirpath):            # override or extend this
  13. method
  14. print dirpath, '...'
  15. def visitfile(self, filepath):          # override or extend this
  16. method
  17. print self.fcount, '=>', filepath
  18. #
  19. class CVisitor(FileVisitor):
  20. def __init__(self, fromdir, todir):
  21. self.fromdirLen = len(fromdir) + 1        # here is my problem
  22. self.todir = todir
  23. FileVisitor.__init__(self, fromdir)
  24. def visitdir(self, dirpath):
  25. topath = os.path.join(self.todir, dirpath[self.fromdirLen:])
  26. os.mkdir(topath)
  27. def visitfile(self, filepath):
  28. topath = os.path.join(self.todir, filepath[self.fromdirLen:])
  29. cpfile(filepath, topath)    #copy contents from filepath to
  30. 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
Feb 2 '07 #1
6 1025
En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gi**@hi.t-com.hrescribió:
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

Feb 2 '07 #2
Gabriel Genellina wrote:
En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gi**@hi.t-com.hrescribió:
>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
Feb 2 '07 #3
Gabriel Genellina wrote:
En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gi**@hi.t-com.hrescribió:
>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
Feb 2 '07 #4
Gigs_ kirjoitti:
hi people

I have problem with this example, not actually the problem, but
Expand|Select|Wrap|Line Numbers
  1. class FileVisitor(object):
  2.     def __init__(self, data=None):
  3.         self.context = data
  4.     def run(self, startdir=os.curdir):
  5.         os.path.walk(startdir, self.visitor, None)
  6.     def visitor(self, data, dirname, filesindir):
  7.         self.visitdir(dirname)
  8.         for fname in filesindir:
  9.             fpath = os.path.join(dirname, fname)
  10.             if not os.path.isdir(fpath):
  11.                 self.visitfile(fpath)
  12.     def visitdir(self, dirpath):            # override or extend this
  13. method
  14.         print dirpath, '...'
  15.     def visitfile(self, filepath):          # override or extend this
  16. method
  17.         print self.fcount, '=>', filepath
  18. #
  19. class CVisitor(FileVisitor):
  20.     def __init__(self, fromdir, todir):
  21.         self.fromdirLen = len(fromdir) + 1        # here is my problem
  22.         self.todir = todir
  23.         FileVisitor.__init__(self, fromdir)
  24.     def visitdir(self, dirpath):
  25.         topath = os.path.join(self.todir, dirpath[self.fromdirLen:])
  26.         os.mkdir(topath)
  27.     def visitfile(self, filepath):
  28.         topath = os.path.join(self.todir, filepath[self.fromdirLen:])
  29.         cpfile(filepath, topath)    #copy contents from filepath to
  30. 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\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
Feb 2 '07 #5
Jussi Salmela wrote:
Gigs_ kirjoitti:
>hi people

I have problem with this example, not actually the problem, but
Expand|Select|Wrap|Line Numbers
  1. class FileVisitor(object):
  2.     def __init__(self, data=None):
  3.         self.context = data
  4.     def run(self, startdir=os.curdir):
  5.         os.path.walk(startdir, self.visitor, None)
  6.     def visitor(self, data, dirname, filesindir):
  7.         self.visitdir(dirname)
  8.         for fname in filesindir:
  9.             fpath = os.path.join(dirname, fname)
  10.             if not os.path.isdir(fpath):
  11.                 self.visitfile(fpath)
  12.     def visitdir(self, dirpath):            # override or extend this
  13. method
  14.         print dirpath, '...'
  15.     def visitfile(self, filepath):          # override or extend this
  16. method
  17.         print self.fcount, '=>', filepath
  18. #
  19. class CVisitor(FileVisitor):
  20.     def __init__(self, fromdir, todir):
  21.         self.fromdirLen = len(fromdir) + 1        # here is my problem
  22.         self.todir = todir
  23.         FileVisitor.__init__(self, fromdir)
  24.     def visitdir(self, dirpath):
  25.         topath = os.path.join(self.todir, dirpath[self.fromdirLen:])
  26.         os.mkdir(topath)
  27.     def visitfile(self, filepath):
  28.         topath = os.path.join(self.todir, filepath[self.fromdirLen:])
  29.         cpfile(filepath, topath)    #copy contents from filepath to
  30. 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\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
Feb 2 '07 #6
"Gigs_" <gi**@hi.t-com.hrescribió en el mensaje
news:ep**********@ss408.t-com.hr...
Gabriel Genellina wrote:
En Thu, 01 Feb 2007 21:33:03 -0300, Gigs_ <gi**@hi.t-com.hrescribió:
>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
Feb 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms...
1
by: Heath | last post by:
I'm dealing with a C# application that monitors changes to the file system, and need to exclude irrelevent directories, temp directories for example. Is there any way to identify such...
2
by: Jeffry van de Vuurst | last post by:
Hi, (sorry for the crosspost, I wasn't sure which was the best place to put this). I was just thinking about something and wondered if any of you has some ideas about this. I'm using the...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
1
by: =?Utf-8?B?R2VuY3k=?= | last post by:
Hi, I'm having a problem the coping cd's etc option is missing in Media Center. It used to be available but it has just disappeared! Can anyone help. Thanks
6
by: =?Utf-8?B?WW9naSBXYXRjaGVy?= | last post by:
Hello, I am using Visual Studio-2003. I created a project to build my library. Since I am using third party libraries as well, I have specified those additional library dependencies in project...
1
oll3i
by: oll3i | last post by:
what are coping constructors and reflexion for ? thank YOU
5
by: Dmitriy Lapko | last post by:
Hallo all Is it possible to change schema of table in DB2 v.8.2 without recreating and coping a table into a new schema? I need it for several purposes, one of them - refactoring of existing...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.