0

I am working on a code to copy images from a folder in a local directory to a remote directory. I am trying to use scp.

So in my directory, there is a folder that contains subfolders with images in it. There are also images that are in the main folder that are not in subfolders. I am trying to iterate through the subfolders and individual images and sort them by company, then make corresponding company folders for those images to be organized and copied onto the remote directory.

I am having problems creating the new company folder in the remote directory.

This is what I have:

for s in subfolder:
    localfile = os.path.normpath(s)
    images = os.listdir(localfile)
    remotehost = "blah@192.168.1.10"
    for image in images:
        name = image.split("-")
        company = name[0]
        remotefile = "/var/files/ImageSync/" + company
        path2 = os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile))
        print(path2)
        if not os.path.exists(path2):
            noFold.append(image)

There's more to the code, and I'll post the rest of it if it makes it easier to understand. The subfolder is a list of the subfolders from the main folder.

I have created a ssh key in hopes of making os.system work, but Path2 is returning 1 when I would like it to be the path to the remote server. (I tried this: How to store the return value of os.system that it has printed to stdout in python?)

Also how do I properly check to see if the company folder in the remote directory already exists?

I have looked at Secure Copy File from remote server via scp and os module in Python and How to copy a file to a remote server in Python using SCP or SSH? but I guess I am doing something wrong.

I'm new to Python so thanks for your help!

Community
  • 1
  • 1
Alina DW
  • 111
  • 1
  • 17
  • 1
    Did you try running your `scp` commands from the command line? – Paul Rooney May 19 '15 at 14:10
  • @PaulRooney I just tried, and it still returns a 1 – Alina DW May 19 '15 at 14:24
  • "how do I properly check to see if the company folder in the remote directory already exists?" SCP isn't a very flexible file-copy protocol. You should look at using SFTP instead. – Kenster May 19 '15 at 18:53
  • @Kenster I'll look into that. Any thoughts on using rsync? Is it possible to use rsync through python? Sorry if this is an irrelevant option – Alina DW May 19 '15 at 19:29
  • No error msg when run from terminal? What about permissions, you'll need those to write into `/var`. Maybe you need to `chown` or `chmod` a directory. – Paul Rooney May 19 '15 at 21:49

0 Answers0