files,folders,paths

Dave Angel davea at ieee.org
Sun Feb 13 14:17:17 CET 2011


On 01/-10/-28163 02:59 PM, ecu_jon wrote:
> i just tried changing that in ver12a, still get
> IOError: [Errno 2] No such file or directory: 'V:\\week2\\configs\
> \apache2.conf'
> good catch tho as that is needed. thanks.
> weekchoice is used to pick week 1-4. really jsut returns 1-4. i plan
> to do full weekly backups with another script.
> getusername i was using when i was trying to build the path as part of
> a unc path. isdir/isfile did not play well with that.
> homedir returns the users home directory. works in xp,7, and linux.
> source and destination should be self explanatory.
> the backupall() gets called from clicking the button. for now its the
> meat and potatoes im working on. it is trying to recursively copy
> files/folders from source to dest. the for ... os.walk line digs
> through the backup folder. i use os.chgdir , os.getcwd and leftover to
> build the path for file/folder names on dest. basically start at c:
> \users\name\backup\somefolder and end with just somefolderadded to
> dest.
> if os.path.isdir(fname): checks if it is a folder. copytree is a
> recursive folder  copy.
> elif os.path.isfile(fname): if it is a file. copy2 copies files and
> metadata.
> class MyForm(wx.Frame): and down is wxpython, that works. ( tho i will
> take suggestions but i know this is not the right forum to as
> questions for that part)
>

I haven't tried to run your script, since much of it is Windows 
dependent, and I'm on Linux.  So observations are just by inspection.

I suspect your problem is that you're trying to change drives as well as 
directories.  In Windows, there's a current directory on each drive, 
that Windows remembers.  So changing directories on a different drive 
doesn't mean what you might think it does.  I'd recommend doing no 
chdir()'s at all, and using only absolute paths.

But I see other complications or maybe risks in the code, concentrating 
on function  backupall().  You're using os.walk(), which walks through a 
whole tree of files.  But then when you encounter a directory, you're 
doing a copytree().  So you'll be copying the same files many-many times.

Similarly, you have logic for copying a single file, but you invoke it 
on a name from dirlist, which is a list of directories, so the isfile() 
probably never fires.

os.walk() assumes the current directory is unchanged during its run. 
See:   http://docs.python.org/library/os.html#module-os
   "walk() never changes the current directory, and assumes that its 
caller doesn’t either."

You're not copying the files at the top level at all;  you don't process 
the list called 'files', while you could probably safely copy *only* the 
files.

Your name manipulations in this same function look risky, using replace 
when you presumably know that a string is either at the begin or end.

In function homeDir(), you never return the value calculated in the try 
block.  If you don't get an exception, you return None.

DaveA





More information about the Python-list mailing list