files,folders,paths

ecu_jon hayesjdno3 at yahoo.com
Sun Feb 13 19:40:08 CET 2011


On Feb 13, 8:17 am, Dave Angel <da... at ieee.org> wrote:
> 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

first let me say, you guys are helping me and i thank you for that. i
do not want to sound confrontational. i have don my best to make this
os agnostic. yes i know, right now in 21a, destination2 is windows
specific. that's mostly because os.path.isdir() and unc paths do not
play well with each other.

the os.chdir() came about out of need to build relative path names.
some examples :
c:\users\name\backup\  #win7
c:\docs and settings\name\app data\backup\  #winxp
/home/name/backup  #linux and mac

i wanted to start from 'backup' in all these cases. my destination is
going to look like
\\servername\username\week[1-4]

yes os.walk goes thru the whole tree (under backup in my case). the
os.chdir() under the for line in def backupall(): basically juct
chages folders to whever the os.walk() is. it never tried to change to
a different drive letter.the folder copy thing, these 2 lines
if os.path.isdir(fname): #if it is a folder
                if not os.path.isdir(destination+leftover): #and id
dont exist on dest drive
work on version 011, determine if its a folder, then if it exists.
only copy if both: it is a folder, and not exist on destination.
this "return homedir" needed to be moved 1 tab left. copy error from
random code to functionalizing parts. good catch thanks.
dirlsit, returns the names of folders and files. i was using a
different method
http://thanksforallthefish.endofinternet.net/date-linuxversion.py

try version 11 for yourself. in your home directory (ie: /home/name)
make a backup folder, put a few files/folders in it. then make a
backup2 folder, and make week1,week2week3week4 folders. version 11
will copy the top level files under /home/name/backup , and recreate
the directory structure. so "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. " atleats for version 11 is not true. atleast not within the
bounds i have tested it.

"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.
"
i know. i have several things that need to build up. like starting
from a relative position and going to a different position. username
would be part of the unc path if i could get that to work, and
week[1-4] is part of the name.

the thing that gets me is version 11 works. well. but its is reading/
writing to almost the same place (from backup, to backup2). defiantly
open to suggestions.



More information about the Python-list mailing list