2016-04-29 10 views
1

ファイルをあるフォルダから別のフォルダに移動したいが、いくつかのファイル例外がある。 1つは特定のファイル名で、その他はAEIOUで始まるすべてのフォルダです。この最後の例外をどうすればできますか?特定のファイル名には既にそれがあります。ありがとう。Python - いくつかの例外を除いて、あるフォルダから他のフォルダにファイルを移動する

import os 
import shutil 

source_dir = "c:/data/abc" 
special_dir = "c:/data/zxy" 

exclude_files =["file.docx", "file2.xls"] 

allfiles = os.listdir(sourcedir) 
for each_file in allfiles: 
    if each_file not in exclude_files: 
     full_path_source_file = os.path.join(source_dir, each_file) 
     full_path_target_file = os.path.join(special_dir, each_file) 
     shutil.move(full_path_source_file, full_path_target_file) 
+1

'os.path.isdir(each_file)及びません( 'AEIOU')をeach_file.startswith' – taras

+0

この行があれば、右の前に来る必要がありますか?ありがとうございます – Gonzalo

+0

@ user128285 'isdir()'はフルパスを渡す必要があります( 'source_dir'が現行の作業ディレクトリでない限り)。 –

答えて

1

独自の例外を定義し、必要に応じてスローすることができます。あなたのfoldersstartinwith AEIOUため 例:

class VocalFolder(Exception) : 

    def __init__(self, path) : 
     self.path = path 

    def __str__(self) : 
     return repr(self.path) 

exclude_filesはAEIOUで始まるフォルダの名前が含まれていることを仮定すると:

try: 
    if each_file not in exclude_files: 
     ## do your stuff here 
    else: 
     raise VocalFolder(each_file) 
except VocalFolder as ex : 
    print "Exception:", str(ex) 

は、この情報がお役に立てば幸いです。

同じ方法でファイルに対して実行できます。あなたはそれはあなたがeach_file[0].upper() not in 'AEIOU'をしたいと思う場合を気にせず母音だかどうかを確認したい場合は、特定の場合にチェックします

import os 
import shutil 

source_dir = "c:/data/abc" 
special_dir = "c:/data/zxy" 

exclude_files =["file.docx", "file2.xls"] 

allfiles = os.listdir(sourcedir) 
for each_file in allfiles: 
    if each_file not in exclude_files and 
     not (os.path.isdir(each_file) and each_file[0] not in 'AEIOU'): 
     full_path_source_file = os.path.join(source_dir, each_file) 
     full_path_target_file = os.path.join(special_dir, each_file) 
     shutil.move(full_path_source_file, full_path_target_file) 

:あなたが欲しい

+0

例外を読むときに例外について考えるのは難しいです...;) – rocksteady

+0

私はそれが理にかなっているフロー制御の例外を使用することに全く反対しませんが、 help:OPが除外されたファイルのために取るべきアクションは、それらをスキップすることです。これは既に 'else:'句を省略するだけです。ここで例外を発生させても何も達成されません。 OPの問題は、その状態を表現しているようだ。 –

+0

あなたは完全に正しいです、私は彼の意図を誤解しています:**いくつかの例外を除いて** – rocksteady

0

あなたはあなたの仕事を支援するため、以下の機能を使用することができます。AEIOUで始まるディレクトリの

あなたの追加の制約は、その後のように表すことができます。 isdir(path) and name.startswith('AEIOU')。ただし、フルパスisdir()に渡す必要がありますが、名前はstartswith()であることを確認してください。したがって、チェックの前に完全なパスを作成し、再度名前を分ける必要があります(basename()を使用)。

しかし、そのすべてをその単一のifステートメントに組み込むと、それはかなり読めなくなります。私はそのための機能でそれを因数分解をお勧めします:

def is_excluded(path): 
    name = basename(path) 
    if isfile(path) and name in ["file.docx", "file2.xls"]: 
     return True 
    if isdir(path) and name.startswith('AEIOU'): 
     return True 
    return False 

チェックをあなたのif声明の中で、単にif not is_excluded(path)になります。


一緒にすべて:

from os.path import basename 
from os.path import isdir 
from os.path import isfile 
import os 
import shutil 


source_dir = "c:/data/abc" 
special_dir = "c:/data/zxy" 


def is_excluded(path): 
    name = basename(path) 
    if isfile(path) and name in ["file.docx", "file2.xls"]: 
     return True 
    if isdir(path) and name.startswith('AEIOU'): 
     return True 
    # Or, if you actually want to check for dirs starting with a vovel: 
    # if isdir(path) and name[0] in 'AEIOU': 
    # return True 
    return False 


allfiles = os.listdir(source_dir) 
for each_file in allfiles: 
    full_path_source_file = os.path.join(source_dir, each_file) 
    full_path_target_file = os.path.join(special_dir, each_file) 
    if not is_excluded(full_path_source_file): 
     shutil.move(full_path_source_file, full_path_target_file) 

編集:私はちょうどあなたが母音ので、個々の文字で始まるディレクトリをチェックしたい場合がありますコメントに基づいて実現['A', 'E', 'I', 'O', 'U']

この場合、新しい小切手は単にname[0].lower() in 'aeiou'(大文字と小文字は区別されません)またはname[0] in 'AEIOU'(大文字と小文字が区別されます)になります。

0

また、次のアプローチを試みることができる:

import os 
import shutil 

source_dir = "c:/data/abc" 
special_dir = "c:/data/zxy" 

exclude_files =["file.docx", "file2.xls"] 
allfiles = os.listdir(sourcedir) # dirs are also included 

for each_file in allfiles: 
    full_path_source_file = os.path.join(source_dir, each_file) 
    full_path_target_file = os.path.join(special_dir, each_file) 

    if os.path.isfile(full_path_source_file): 
     if each_file not in exclude_files: 
     shutil.move(full_path_source_file, full_path_target_file) 
    else: # each_file is a folder 
     if not each_file.upper().startswith('AEIOU'): 
     shutil.move(full_path_source_file, full_path_target_file) 
関連する問題