Wednesday, July 28, 2010

Unix Copying Whining

Why is it so wrong of me to want to copy files in the following way? (in iPython)

for item in to_save:
   ....:     try:
   ....:         folder = os.path.split(item)[0]
   ....:         os.makedirs("backup_folder/" + folder)
   ....:     except:
   ....:         pass
   ....:     !cp $item folder/$item

This would be ideal for me.

[The issue I'm complaining about is that when you copy a file in Unix, the target location must already exist for the file.  Python's os.makedirs() makes intermediary folders (unlike the similar os.makedir())--but, in neither Python nor Bash can I find a function that copies, while making those intermediary folders.  I can't imagine a danger in this, but seem to want to do it while backing up, all of the time.]

3 comments:

  1. Try this for your BASH line:

    !rsync -avuzb $item folder/

    ReplyDelete
  2. I don't know much about rsync, actually, but isn't the idea with it, to keep things syncing later?

    That might be interesting, though... so many attribute files in so many places, both in svn and out of it now...

    ReplyDelete
  3. Yeah, this is of course not the intended purpose of rsync, but it can work. As far as I know there isn't a natural one line you can write that will create the directory structure while copying files. I think because such things as "mkdir -p" and "os.makedirs()" exist, they never bothered to implement it for files because it is easy enough to just use those calls.

    ReplyDelete