files,folders,paths

Dave Angel davea at ieee.org
Mon Feb 14 02:48:22 CET 2011


(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



More information about the Python-list mailing list