4

私は Python の初心者ですが、すべてのダウンロードを並べ替えるプログラムを作成するのは楽しいと思いましたが、少し問題があります。目的地に単語が 1 つしかない場合は完全に機能しますが、目的地に単語が 2 つ以上ある場合は、ここで問題が発生し、プログラムがループに陥ります。リストを比較するのに私よりも優れたアイデアを持っている人はいますか

>>>for i in dstdir:
>>>    print i.split()

['CALIFORNICATION']
['THAT', "'70S", 'SHOW']
['THE', 'BIG', 'BANG', 'THEORY']
['THE', 'OFFICE']
['DEXTER']
['SPAWN']
['SCRUBS']
['BETTER', 'OF', 'TED']

>>>for i in dstdir:
>>>    print i.split()
['Brooklyn.Nine-Nine.S01E16.REAL.HDTV.x264-EXCELLENCE.mp4']
['Revolution', '2012', 'S02E12', 'HDTV', 'x264-LOL[ettv]']]
['Inequality', 'for', 'All', '(2013)', '[1080p]']

これはリスト出力の例です。

フォルダのみを含む宛先ディレクトリと、ダウンロード ディレクトリがあります。ソースファイル名を自動的に調べてから、宛先名を調べるプログラムを作成したいと思います。宛先名がソース名に含まれている場合は、ダウンロードしたファイルをコピーしてコレクションに並べ替えることができます。

destination = '/media/mediacenter/SAMSUNG/SERIES/'
source = '/home/mediacenter/Downloads/'
dstdir = os.listdir(destination)
srcdir = os.listdir(source)

for i in srcdir:
    source = list(i.split())
    for j in dstdir:
        count = 0
        succes = 0
        destination = list(j.split())
        if len(destination) == 1:
            while (count < len(source)):
                if destination[0].upper() == source[count].upper():
                    print 'succes ', destination, ' ', source
                count = count + 1
        elif len(destination) == 2:
            while(count < len(source)):
                if (destination[0].upper() == source[count].upper()):
                    succes = succes + 1
                    count = len(source)
            count = 0
            while(count < len(source)):
                if (destination[1].upper() == source[count].upper()):
                    succes = succes + 1
                    count = len(source)
            count = 0
            if succes == 2:
                print 'succes ', destination, ' ', source

今のところ、出力として「成功」だけで満足しています。近い将来、私にとってはまったく別の問題になるので、ファイルをコピーする方法を見つけます

4

4 に答える 4

2

このようなものかもしれません。宛先フォルダー内のすべての単語がファイル名に存在するかどうかを確認します

dstdir = ['The Big Bang Theory', 'Dexter', 'Spawn' ]

srcdir = ['the.big.bang.theory s1e1', 'the.big.bang.theory s1e2', 'dexter s2e01']

for source in srcdir:
    for destination in dstdir:
        destinationWords = destination.split()

        if all(word.lower() in source.lower() for word in destinationWords):
            print 'succes ', destination, ' ', source

出力:

succes  The Big Bang Theory   the.big.bang.theory s1e1
succes  The Big Bang Theory   the.big.bang.theory s1e2
succes  Dexter   dexter s2e01
于 2014-02-15T15:33:44.983 に答える
0

以下に提案するこの単純なスクリプトを使用して、ファイルをソースから宛先に移動します。

src = "/home/mediacenter/Downloads"
dst = "/media/mediacenter/SAMSUNG/SERIES"
source =  os.listdir(src)
destination = os.listdir(dst)

for filename in source:

    file_src = src +"/"+ str(filename)
    file_dst = dst +"/"+ str(filename)

    if filename not in destination and os.path.isdir(file_src) is False:
        #download file
        os.system("mv %s %s" %(file_src, file_dst))
    elif filename not in destination and os.path.isdir(file_src) is True:
        #download directory
        os.system("mv %s %s" %(file_src, dst))

それはあなたが探しているもののようです。ファイル名が宛先リストにないかどうかを確認して移動するだけです。それはあなたのために働きましたか?

于 2014-02-15T16:24:26.637 に答える