files,folders,paths

ecu_jon hayesjdno3 at yahoo.com
Sun Feb 13 23:02:46 EST 2011


On Feb 13, 8:48 pm, Dave Angel <da... at ieee.org> wrote:
> (You forgot to include the list in your response, and you neglected to
> properly quote the message you were replying to.  That makes it quite
> hard to separate your remarks from mine)
>
> On 02/13/2011 07:19 PM, jon hayes wrote:
>
> > c:\users\name\backup\  #win7
> >> c:\docs and settings\name\app data\backup\  #winxp
> >> /home/name/backup  #linux and mac
>
> > In what way do those imply a need for chdir() ?  What's wrong with os.path.join() ?
> > the only commonality is the \backup
> > the chdir() is there to build the path name of the destination.
> > so c:\users\name\backup\folder1\folder2\file3
> > in my code, doing the substution.
>
> Well, since chdir() is illegal inside the os.walk() context and
> unnecesary, I suggest you debug your original technique.  Build the
> names with os.path.join(), as I've suggested twice before, and others
> have mentioned as well.
>
>
>
> > these 3 lines
> > os.chdir(path)
> > cwd = os.getcwd()
> > leftover = cwd.replace(source, '')
> > in this case leftover = \folder1\folder2
> > which gets added to the path for the destination in
> > shutil.copytree(os.getcwd(), destination+leftover)
>
> > i looked at the relpath, it did not do exacly what i wanted. in version 11 of the code this works out well.
>
> I can't see how os.path.relpath() would be useful here.
>
>
>
> > "And I quote from facbac-012a.py:
> >          os.chdir("v:")"
> > i don't understand. is this a question?
>
> It is in context.  If you read my message, I had just quoted you saying
> you did not change drive letters.
>
>
>
> > "I suggest you actually try writing a simple loop using os.walk to just
> > display the names of all the files in and under a specified directory.
> > Then maybe you'll understand what  path, dirs, and files actually are."
> > i started with that. look at the attached file, date-linuxversion.py.
> > the relative paths are not, quite, right.
>
> Well file you enclosed shows that you don't understand os.walk().  So
> learn it before trying to use it in a more complex setting.  Why would
> you use the following sequence?
>
>      filestring = str(files)
>      filestring = filestring.strip("[")
>      filestring = filestring.strip("]")
>      filestring = filestring.strip("'")
>
> This is no way to process a list of filenames.
>
> > the thing with the copytree is, the line above is sopposed to check whether that folder exists. if it does not exist at all, then why not copy the whole thing?
> > same sort of thing with files. if not exist then do copy. that way nothing should get clobbered.
> > this is the observed behavior in version 11.
>
> Why not copy the whole tree?  Because once the code works, you'll be
> copying it many times.
>
> DaveA

if i do not change the directory how do i get the relative folder
path. for example,
starting with c:\users\name\backup\folder1\folder2\file
as the oswalk drills down through the folders, when it is in the
'folder2' folder, if you do something like
print dirs
it would output just 'folder2' not the whole path or any of the other
parts.
so how do i know where to write this to on the destination relative
to
\\servername\username\week[1-4]\ or possibly that folder mounted as a
drive in windows as
v:\week[1-4]\ ?
how do i build the output folder name.
bear in mind it works great in version 011.
i spent weeks, and tried maybe 7 different other ways before going
down this path, so to speak.
i thought about using a list. or an array. but you wouldnt know when
oswalk starts bak from top level to go int another folder. let me
think if i can say it another way. ...
i think what you guys want me to do the osjoin on is this line
destination = r"\\\\Mothera\\"+username+"\\"+weekstr   or
shutil.copytree(os.getcwd(), destination+leftover)
please clarify.



More information about the Python-list mailing list