Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:30:07+00:00 2026-06-15T18:30:07+00:00

I’m trying to write a python script to copy files from a remote server

  • 0

I’m trying to write a python script to copy files from a remote server to a local directory via scp.

Because I’m running this on an OpenELEC distribution (minimal HTPC linux distro, read-only filesystem except for userhome makes it impractical to install python ssh module), I’m doing this ugly and just passing the filename to the scp command via os.system.

SCPCopy = "scp -c blowfish -C user@host:\"" + pipes.quote(file) + "\" /storage/downloads/incoming/"
SCPCopy = SCPCopy.replace('\n','')
os.system(SCPCopy)

This works, except for filenames containing an apostrophe.

Below is an example of what gets passed to os.system in a file with an apostrophe:

scp -c blowfish -C user@host:"'/media/sdi1/home/data/bob'"'"'s file.avi'" /storage/downloads/incoming/

And the error:

sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

It looks pipes.quote(x) is escaping the apostrophe (as it should), but obviously the syntax is still incorrect. I’ve experimented ditching pipes.quote(x) and replacing apostrophes with /’ but that isn’t getting me anywhere either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:30:08+00:00Added an answer on June 15, 2026 at 6:30 pm

    As scp is based on SSH, the filenames you give to it are subject to shell escaping on the remote side as well. Thus you need to escape twice.

    A correctly escaped cmdline for the shell:

    scp -c blowfish -C user@host:"\"/media/sdi1/home/data/bob's file\"" /storage/.../
    

    To make a python string, we have to add one more level of escaping. To stay sane, we could use triple-quotes:

    """scp -c blowfish -C user@host:"\"/media/sdi1/home/data/bob's file\"" /storage/.../"""
    

    If you do it programmatically (e.g. using the deprecated pipes.quote), then don’t touch the filename at all (in your example above, you added apostrophes around the filename).

    fp = "/media/sdi1/home/data/bob's file.avi"
    fp = "user@host:" + pipes.quote(pipes.quote(fp))
    
    cmdline = "scp -c blowfish -C " + fp + " /storage/downloads/incoming/"
    os.system(cmdline)
    

    This is admittedly confusing. For a simple model, the whole point of pipes.quote is to escape the input so that the input will be parsed by the shell as exactly one word, which is equal to the input.

    The following is a more generally correct way (and yields the same result):

    fp = "/media/sdi1/home/data/bob's file.avi"
    # the filepath argument escaped for ssh/scp on the remote side
    fp = pipes.quote(fp)
    commandargs = ["scp", "-c", "blowfish", "-C", "user@host:"+fp, "/storage/downloads/incoming/"]
    # escape all words for the local shell, and then concatenate space-separated
    cmdline = " ".join(map(pipes.quote, commandargs))
    os.system(cmdline)
    

    It expresses more clearly the intent: Controlling what words exactly the shell will parse.

    But why start with a shell in the first place? We don’t need one and can save the escaping on the local side. To spawn a process with our args, directly, use commands from the os.exec* family.

    fp = pipes.quote("/media/sdi1/home/data/bob's file.avi")
    commandargs = ["scp", "-c", "blowfish", "-C", "user@host:"+fp, "/storage/downloads/incoming/"]
    if os.fork() == 0:
        os.execvp("scp", commandargs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.