2014-09-18 75 views
0

我有一个名为source的列表,用于存储文件名。现在基于每个文件的扩展名,我希望它复制到不同的文件夹。我知道这是一个非常简单的任务,但不知何故,我没有得到它。她是我的代码:使用Python将文件复制到基于扩展名的不同目录中

import shutil , os 

source = ['test_sound.wav','ts.raw'] 

src= '/home/GM/codec_implement/Audio_files' 

destination1= '/home/GM/codec_implement/raw_files' 

destination2= '/home/GM/codec_implement/raw_files/wave_files' 

for files in source: 

fileName, fileExtension = os.path.splitext(files) 

    if (fileExtension=='.raw'): 
     full_filename = os.path.join(source, files) 
     shutil.copy(full_filename,destination1) 

    elif (fileExtension=='.wav'): 
     full_filename = os.path.join(source, files) 
     shutil.copy(full_filename,destination2) 

    else: 
     print "This is not a valid file format " 

错误:我得到这样意外的缩进错误都是我不的时候明白的地方,我会犯错,我使用Python 2.7

+0

'''文件名,fileExtension = os.path.splitext(文件)'''应缩进。 – wwii 2014-09-18 18:05:15

回答

1

您需要将线fileName, fileExtension = os.path.splitext(files)四个空格来正确的。

看看pylint。它会告诉你错误在哪里。

例如,运行pylint对你的代码:

pylint -r n /tmp/foo 

************* Module foo 
E: 13,0: expected an indented block 
相关问题