Python scp复制文件,文件名中带有空格 [英] Python scp copy file with spaces in filename

查看:395
本文介绍了Python scp复制文件,文件名中带有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scp在本地网络中复制文件. 它可以与没有空格的文件名一起很好地工作,但是会崩溃. 我试图用"\"代替",因为这

I'm trying to copy files in local network with scp. It's working well with filenames without spaces, but it crash with. I've tried to replace " " with "\ " as this exemple, but it don't work. Here is my code:

def connection(locals):
         a = (int(re.search(br'(\d+)%$', locals['child'].after).group(1)))
         print a
         perc = (Decimal(a)/100)
         print (type(perc)), perc
         while gtk.events_pending():
             gtk.main_iteration()
         FileCopy.pbar.set_text("Copy of the file in the Pi...   " + str(a) + "%")
         while gtk.events_pending():
             gtk.main_iteration()
         FileCopy.pbar.set_fraction(perc)

file_pc = "/home/guillaume/folder/a very large name of file with space .smthg"
file_pi = "pi@192.168.X.X:/home/pi/folder/a very large name of file with space .smthg"

if " " in file_pc:
   file_pc = fichier_pc.replace(" ", '\\\ ')   # tried '\\ ' or '\ '
   file_pi = fichier_pi.replace(" ", '\\\ ')   # but no way
else:
   pass
command = "scp %s %s" % tuple(map(pipes.quote, [file_pc, file_pi]))
pexpect.run(command, events={r'\d+%': connection}) # this command is using to get the %

如何解决此问题? 谢谢

How can I fix this problem ? Thanks

推荐答案

您可以将本地文件file_pc保持不变(pipes.quote将转义空格).远程文件应更改:

You may keep local file file_pc as is (pipes.quote will escape the spaces). The remote file should be changed:

import pipes

file_pi = 'pi@192.168.X.X:/home/pi/folder/file with space.smth'
host, colon, path = file_pi.partition(':')
assert colon
file_pi = host + colon + pipes.quote(path)

,即user@host:/path/with space应该更改为user@host:'/path/with space'

这篇关于Python scp复制文件,文件名中带有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆