0

I'm doing a program that copies some files from a music playlist. I execute the command just like this:

command =   'cp "%s" "%s"' % (songPath,plPath)
os.system(command)

The problem is that when I execute that if the song's path has a ' character command can not be executed. It says:

cp: cannot stat `/home/myname/Music/Oasis/(What\'s The Story) Morning Glory/03 Wonderwall.mp3': No such file or directory

I checked the songPath and has no \ character before the ' Does anyone know how to avoid the program adding that \ character?

Thank you in advance!

1 Answers1

6

Use subprocess.call instead:

ret_val = subprocess.call(['cp',songPath,plPath])

This avoids the shell so your arguments should be passed the cp in the exact form that you gave them.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I have the same problem with subprocess.call, but shutil.copy2(songPath,plPath) solved the problem (thanks to @loganfsmyth) –  May 03 '13 at 09:52