Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Moving folders with content

Reply
Thread Tools

RE: Moving folders with content

 
 
Guest
Posts: n/a
 
      09-16-2012
Thank you "Nobody" and Hans!

> You may want to use the subprocess module to run 'ditto'. If
> the destination folder does not exist, then ditto will copy MacOS
> specific aspects such as resource forks, ACLs and HFS meta-data.


This looks like a good direction to go. Maybe something like:

>>> import os
>>> import subprocess
>>>
>>> p1 = os.path.expanduser('~/Desktop/IN/Test/')
>>> p2 = os.path.expanduser('~/Desktop/OUT/Test/')
>>>
>>> cmd = 'ditto -vV "' + p1 + '" "' + p2 + '"'
>>>
>>> v = subprocess.check_output(cmd, shell=True)
>>> Copying /Users/jay/Desktop/IN/Test/

copying file ./.DS_Store ...
6148 bytes for ./.DS_Store
copying file ./dude.txt ...
4 bytes for ./dude.txt
copying file ./new.png ...
114469 bytes for ./new.png

Jay
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      09-16-2012
On 16/09/12 10:02:09, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Thank you "Nobody" and Hans!


You're welcome!

>> You may want to use the subprocess module to run 'ditto'. If
>> the destination folder does not exist, then ditto will copy MacOS
>> specific aspects such as resource forks, ACLs and HFS meta-data.

>
> This looks like a good direction to go. Maybe something like:
>
>>>> import os
>>>> import subprocess
>>>>
>>>> p1 = os.path.expanduser('~/Desktop/IN/Test/')
>>>> p2 = os.path.expanduser('~/Desktop/OUT/Test/')
>>>>
>>>> cmd = 'ditto -vV "' + p1 + '" "' + p2 + '"'
>>>>
>>>> v = subprocess.check_output(cmd, shell=True)


This looks iffy: it would break if there would be any double
quotes in p1 or p2. You might think that os.path.expanduser
would never expand '~' to something containing a double quote,
but you'd be wrong:

>>> import os
>>> os.environ['HOME'] = 'gotcha!"; rm -rf '
>>> print(os.path.expanduser('~/Desktop/IN/Test/'))

gotcha!"; rm -rf /Desktop/IN/Test/

It's easy and safer to avoid using 'shell=True' option:

cmd = ['ditto', '-vV', p1, p2]
v = subprocess.check_output(cmd, shell=False)

In this case, the safer version also happens to be shorter and
more readable. But you should get into the habit of using
shell=False whenever possible, because it is much easier to
get it right.


Hope this helps,

-- HansM
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      09-16-2012
On Sun, 16 Sep 2012 12:40:18 +0200, Hans Mulder wrote:

> But you should get into the habit of using shell=False whenever
> possible, because it is much easier to get it right.


More accurately, you should get into the habit of passing a list as the
first argument, rather than a string.

On Unix-like systems (including Mac OS X), this effectively requires
shell=False. Passing a list with shell=True has behaviour which is
well-defined, but rarely useful (the first element of the list will be
executed as a shell command, the remaining elements will be available via
the shell variables $1, $2, etc within that command).

On Windows, the list is converted to a command string using the same
quoting rules regardless of the value of the shell= parameter. The
difference is that shell=False requires the "executable" to actually be a
binary executable, while shell=True allows it to be some other type of
file (e.g. a batch file, Python script, etc).

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-16-2012
On Sun, 16 Sep 2012 15:31:07 +0100, Nobody <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:


> difference is that shell=False requires the "executable" to actually be a
> binary executable, while shell=True allows it to be some other type of
> file (e.g. a batch file, Python script, etc).


While it is true that "programs" that require an interpreter to be
present won't run directly, the main difference is that "shell=True"
allows for use of the built-ins -- commands like DIR, COPY, DEL...

One can always preface the others with the interpreter program name
(granted, in the case of "BAT" files, that interpreter IS the command
shell, so it if rather perverse to say "run this file in this shell"
while saying "don't use a shell" <G>).


--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-16-2012
On Sun, 16 Sep 2012 14:53:02 -0400, Dennis Lee Bieber
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:

> On Sun, 16 Sep 2012 15:31:07 +0100, Nobody <(E-Mail Removed)>
> declaimed the following in gmane.comp.python.general:
>
>
> > difference is that shell=False requires the "executable" to actually be a
> > binary executable, while shell=True allows it to be some other type of
> > file (e.g. a batch file, Python script, etc).

>
> While it is true that "programs" that require an interpreter to be
> present won't run directly, the main difference is that "shell=True"
> allows for use of the built-ins -- commands like DIR, COPY, DEL...
>
> One can always preface the others with the interpreter program name
> (granted, in the case of "BAT" files, that interpreter IS the command
> shell, so it is rather perverse to say "run this file in this shell"
> while saying "don't use a shell" <G>).


I may have over trimmed the quote -- the preceding comment applies
to usage on M$ Windows... (with one typo corrected)
--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving folders with content Python 2 09-15-2012 10:52 AM
Re: Moving folders with content Laszlo Nagy Python 0 09-15-2012 06:36 AM
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
Syncing POP folders with IMAP folders Rich Computer Support 1 02-12-2004 09:36 PM



Advertisments