2016-04-29 117 views
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),而不是each_file.startswith( 'AEIOU')' – 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) 

希望这有助于。

以同样的方式你可以为文件做到这一点。

+0

在读取异常时,我们很难不去考虑异常......;) – rocksteady

+0

我并不反对在流量控制中使用异常,但是在这里它根本没有意义, t帮助:OP想要排除的文件的操作是*跳过*它们,这已经完成了,只是省略了'else:'子句。在这里引发异常并没有完成任何事情。 OPs问题似乎与表达条件有关。 –

+0

你是完全正确的,我只是误解了他的意图:**有一些例外** – rocksteady

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) 
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) 

,将与特定的情况下检查,如果你要检查它是否是没有照顾元音的情况下,你会想这样做each_file[0].upper() not in 'AEIOU'

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) 
相关问题