Skip to content Skip to sidebar Skip to footer

Python Scp Copy File With Spaces In Filename

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

Solution 1:

Use subprocess module and/or shlex.split():

import subprocess
subprocess.call(['scp', file_pc, file_pi])

and you don't need to worry about escaping or quoting anything


Solution 2:

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)

i.e., user@host:/path/with space should be changed to user@host:'/path/with space'


Solution 3:

You might want to look into fabric, a Python library that streamlines the use of SSH.

from fabric.state import env
from fabric.operations import get

env.user = 'username'
env.key_filename = '/path/to/ssh-key'

get('/remote_path/*', 'local_path/')

Post a Comment for "Python Scp Copy File With Spaces In Filename"