玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python多个目录更新问题求助

from subprocess import call
import os

def consistence(sourcedir, destinationdir):
    sr = set(os.listdir(sourcedir))
    de = set(os.listdir(destinationdir))

    os.chdir(sourcedir)

    for i in sr.difference(de):
        call(('rm', '-rf', i))

    for i in sr.intersection(de):
        if os.path.isdir(i):
            consist(sourcedir + '/' + i, destinationdir + '/' + i)

    for i in de.difference(sr):
        call(('cp', '-r', destinationdir + '/' + i, sourcedir))

我想将两个目录更新,但是两个目录下的相同目录就不行,不能更新共有目录下的文件

#!/usr/bin/env python

from subprocess import call
import os
import sys

def consistency(sourcedir, destinationdir):
    sourcedir = unicode(sourcedir)
    destinationdir = unicode(destinationdir)

    sr = set(os.listdir(sourcedir))
    de = set(os.listdir(destinationdir))

    if not sr and not de:
        return

    os.chdir(sourcedir)

    for i in sr.difference(de):
        call(('rm', '-rf', i))

    for i in sr.intersection(de):
        if os.path.isdir(sourcedir + '/' + i) and os.path.isdir(destinationdir + '/' + i):
            consistency(sourcedir + '/' + i, destinationdir + '/' + i)

    for i in de.difference(sr):
        call(('cp', '-r', destinationdir + '/' + i, sourcedir))


def main():
    if len(sys.argv) > 3:
        print "Error: at most 3 arguments"
        print "Usage: consistency(source, destination)"
        return 1

    sourcedir = sys.argv[1]
    destinationdir = sys.argv[2]
    consistency(os.path.abspath(sourcedir), os.path.abspath(destinationdir))

if __name__ == '__main__':
    main()

搞掂

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd18897.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月23日 - 16时57分47秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules