質問

In a small wxPython programme I'm making I have a for loop that takes items in a list of files and copies them using cp copy. Everything is fine and works on sources without spaces in their paths. However when sources are introduced the cp function cannot find my source. Here is my code:

if f_name in selectedsecondcource:
    self.counter += 1
    self.toPrint = self.counterText + str(self.counter) + "/" + str(len(selectedList))
    self.oncopy.SetLabel(self.toPrint)
    src_abs_path = os.path.join(r, file)
    src_relative_path = os.path.relpath(src_abs_path, self.twoDirFilename)
    dst_abs_path = os.path.join(self.DirDest, src_relative_path)
    dst_dir = os.path.dirname(dst_abs_path)
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    ret = os.system('cp -fr %s %s' % (src_abs_path, dst_abs_path))

Any help is appreciated. Although this is my first Python programme my understanding of Python is better than thisos.system('cp... stuff.

In summery: How do I make this work on sources with spaces in their directory names?

Thanks Gavin

役に立ちましたか?

解決

Normal, if the filenames are separated by a space, the command wont work.

Try with quotes:

ret = os.system('cp -fr "%s" "%s"' % (src_abs_path, dst_abs_path))

But this is better: http://docs.python.org/2/library/shutil.html#shutil.copy

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top