os.system创建远程目录(Python)

时间:2015-05-19 13:35:31

标签: python ssh directory scp

我正在编写一个代码,用于将图像从本地目录中的文件夹复制到远程目录。我正在尝试使用scp。

因此,在我的目录中,有一个文件夹包含其中包含图像的子文件夹。主文件夹中还有不在子文件夹中的图像。我试图遍历子文件夹和单个图像并按公司排序,然后为这些图像组成相应的公司文件夹并将其复制到远程目录中。

我在远程目录中创建新公司文件夹时遇到问题。

这就是我所拥有的:

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)

代码还有更多内容,如果它更容易理解,我会发布剩下的代码。 subfolder是主文件夹中子文件夹的列表。

我已经创建了一个ssh密钥以期使os.system正常工作,但当我希望Path2成为远程服务器的路径时,它返回1。 (我试过这个:How to store the return value of os.system that it has printed to stdout in python?

另外,如何正确检查远程目录中的公司文件夹是否已存在?

我看过Secure Copy File from remote server via scp and os module in PythonHow to copy a file to a remote server in Python using SCP or SSH?,但我想我做错了。

我是Python的新手,感谢您的帮助!

0 个答案:

没有答案