hi people
I have problem with this example, not actually the problem, but -
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 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
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
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
Gigs_ kirjoitti:
hi people
I have problem with this example, not actually the problem, but -
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\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
Jussi Salmela wrote:
Gigs_ kirjoitti:
>hi people
I have problem with this example, not actually the problem, but - 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\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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: oll3i |
last post by:
what are coping constructors and reflexion for ?
thank YOU
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |