On 04/26/2013 08:10 AM, mike wrote:
Hi all,
I wrote a cli script for syncing my music to a USB mass storage device
like a phone etc. all it does is it creates a symlink in a dir to
whatever folder i pass as an argument via ./addsync DIR . My problem is
i'm having trouble dealing with directories with spaces. when using
os.symlink I get the error below and when using subprocess it creates
individual directories for the words separated by white space. any help
would be appreciated on how to approach this.thanks!
This is the script:
#!/usr/bin/env python
import os
from sys import argv
import subprocess
script, media = argv
# This is the directory you will sync the symbolic links from, to the
device.
loadDir = "/opt/data/music/load/"
# This is the destination folder, whatever folder the device is mounted to.
destDir = "/media/MOT_"
# Get the current working directory
origDir = os.getcwd()
# Glob the current working directory and the "media" argument together
# to form a full file path
origFul = os.path.join('%s' % origDir, '%s' % media)
linkDir = "ln -s %s %s" % (origFul, loadDir)
# Command to sync if media == "push"
syncCom = "rsync -avzL %s %s" % (loadDir, destDir)
def link():
print "Adding %s to %s..." % (origFul, loadDir)
os.symlink('origFul', 'media')
# subprocess.call(linkDir, shell=True)
By using shell=True, you're forcing your OS's shell to interpret the
line. If you insist on that approach, you'll need to escape any
embedded spaces, same as you would in the terminal.
Easier and safer would be to pass the list of argument to
subprocess.call, the way these docs show:
http://docs.python.org/2/library/subprocess.html#subprocess.call
So instead of linkDir looking like "ln -s file 1 file2" it should look
like:
["ln", "-s", "file 1", "file2"]
and you'd use it like:
subprocess.call(linkDir)
Avoiding the shell=True is faster, safer, and usually much easier.
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor