files,folders,paths

Dave Angel davea at ieee.org
Mon Feb 14 04:43:02 EST 2011


On 01/-10/-28163 02:59 PM, ecu_jon wrote:
> this is a reply to both Dave Angel and Ben Finney. this version of
> testing i think incorperates what you guys are saying.
> <a href="http://thanksforallthefish.endofinternet.net/
> testing1.py">testin1.py</a>
> except maybe the os.join.path in the last function.
> here is the traceback
> Traceback (most recent call last):
>    File "I:\college\spring11\capstone-project\testing.py", line 88, in
> <module>
>      backupall()
>    File "I:\college\spring11\capstone-project\testing.py", line 84, in
> backupall
>      shutil.copy2(filesource, filedest1)
>    File "C:\Python27\lib\shutil.py", line 127, in copy2
>      copyfile(src, dst)
>    File "C:\Python27\lib\shutil.py", line 82, in copyfile
>      with open(dst, 'wb') as fdst:
> IOError: [Errno 2] No such file or directory: '//Mothera/week2\\jonI:\
> \college\\spring11\\capstone-project\\changelog.txt'
>
> it looks like it needs os.walk and the other for line to dig through
> the folders.
> this being the whole path
> I:\\college\\spring11\\capstone-project\\changelog.txt of source.
>

Simplify the code.


def weekChoice()
     return 1 + (datetime.now().day -1) // 7   #weeks go from 1 to 5

def backupall():
     source = source1()
     destination = destination2()
     print "copy tree ", source, destination
     if os.path.exists(destination):
         if os.path.isdir(destination):
             shutil.rmtree(destination)
	else:
             os.unlink(destination)
     shutil.copytree(source, destination)
     return

All that nonsense with curdir was messing up your thinking.


I'm not going to try to debug your destination2() function, but I would 
point out that when you use os.path.join(), please let it do the work 
for you.  It takes any number of arguments, and those arguments should 
not have extra slashes or backslashes in them.  The leading slashes are 
okay for the first node, if you want a unc.

It would have been much easier if you had made source and destination 
arguments to the backupall() function.  It then might have become clear 
that it's simply copytree(), with the extra requirement of deleting 
whatever used to be there.

Now if you aren't allowed to delete what was already there, then you 
can't use copytree, and need to start over with os.walk().

DaveA



More information about the Python-list mailing list