nprak
nprak

Reputation: 343

Copy files to a different directory based on their extension using Python

I have a list named source where I store the file names. Now based on the extension of each file I want it to copy to different folders. I know it is a very simple task but somehow i am not getting it. Her is my code:

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 "

Errors: I get errors like unexpected indentation all the time I dont understand where I am making mistake and I am using Python 2.7

Upvotes: 0

Views: 90

Answers (1)

Vor
Vor

Reputation: 35109

You need to move line fileName, fileExtension = os.path.splitext(files) four spaces to the right.

Take a look at the pylint. It will show you where the error is.

for example, running pylint against your code:

pylint -r n /tmp/foo

************* Module foo
E: 13,0: expected an indented block

Upvotes: 1

Related Questions