1

それで、shutilモジュールを使用してファイルソートプログラムを作成しようとしています

import glob, shutil, os, time, argparse


src='' 
dest=''
ext=''
extention = ''


def parser():
    parser = argparse.ArgumentParser(description='Grab shell flags')
    parser.add_argument('--src', action="store", dest="src", type=str, default=None)
    parser.add_argument('--dest', action="store", dest="dest", type=str, default=None)
    parser.add_argument('--ext', action="store", dest="ext", type=str, default=None)
    global src 
    global dest
    global ext

def if_parser_fails(source_directory, dest_directory, file_extentions):
    while source_directory is None:
            global src
            src = raw_input('What is the directory you wish to monitor?: ')
    while dest_directory is None: 
            global dest
            dest = raw_input('Where do you wish to move the files?: ')
    while file_extentions is None:
            global extention
            extention = raw_input('Please input the extension(s), seperated by commas, that you wish to use as criteria for sorting: ')


def sorter(source_directory, dest_directory, file_extentions): 
    found = glob.glob('%(src)s./*' % {"src": source_directory})
    ext_list = file_extentions.split(',')
    global moving_files
    moving_files = []
    for i in found:
            (filepath, filename) = os.path.split('%(file)s' % {"file": i})
    for h in filename:
            for j in ext_list:
                    if  j in h:
                            moving_files.append(h)
    else:
            pass

def copy_files(to_move, source_directory, dest_directory):
    for item in to_move:
            full_path = os.path.join(source_directory, item)
            full_path_dest = os.path.join(dest_directory, item)
            shutil.move(full_path, full_path_dest)
            print("%(file)s has been copied to %(dest)s" % {"file": i, "dest": dest_directory})

def log_writer():
    log_path = os.path.join(dest, 'log.txt')
    write_time = time.asctime()

    with open(log_path, 'ab') as logfile:
            logfile.write('%(filename)s has been moved \n %(time)s \n \n' % {"filename": filename, "time": write_time})
    print file(log_path).read()

def main():
    parser()
    if_parser_fails(src, dest, ext)
    sorter(src, dest, extention)
    copy_files(moving_files, src, dest)
    log_writer()
if __name__ == "__main__":
    main()

しかし、shutil はエラーを出し続けます:

    shutil.Error: `S` and `S` are the same file

入力ディレクトリと出力ディレクトリに何らかの値が割り当てられていないことに関係があると思います。

どんな助けでも大歓迎です。

4

1 に答える 1

1

完全なトレースバックがないとわかりにくいですが、このセグメントでは次のようになります。

for i in found:
        (filepath, filename) = os.path.split('%(file)s' % {"file": i})
for h in filename:
        for j in ext_list:
                if  j in h:
                        moving_files.append(h)

最後のファイル名のみを取得し、そのファイル名の個々の文字を移動ファイルに追加します。

代わりに試してください:

for i in found:
        (filepath, filename) = os.path.split('%(file)s' % {"file": i})
        for h in filename:
                for j in ext_list:
                        if  j in h:
                                moving_files.append(h)
于 2012-08-14T20:09:10.813 に答える