[Tutor] symlinking dirs with spaces

mike mike at froward.org
Fri Apr 26 14:10:03 CEST 2013


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)

def sync():
     print "Syncing the contents of %s to %s" % (loadDir, destDir)
     subprocess.call(syncCom, shell=True)

if media == "push":
     sync()
else:
     link()



rev at sheridan:~/code$ ./addsync SPACES\ TEST/
Adding /home/rev/code/SPACES TEST/ to /opt/data/music/load/...
Traceback (most recent call last):
   File "./addsync", line 37, in <module>
     link()
   File "./addsync", line 27, in link
     os.symlink('origFul', 'media')
OSError: [Errno 17] File exists
rev at sheridan:~/code$ ls -lh /opt/data/music/load/
total 4.0K
-rwxrwxr-x 1 rev rev 225 Apr 11 09:26 addsync
lrwxrwxrwx 1 rev rev  34 Oct 29 20:30 CCR - Best of CCR -> 
../classic_rock/CCR - Best of CCR/
rev at sheridan:~/code$



More information about the Tutor mailing list