NewInPython
NewInPython

Reputation: 247

how can i automate copying over files by giving a condition using python?

Im new to python, i have a script-

import glob
    import os
    import shutil
    import pandas as pd 
    df = pd.read_fwf(r'factadwords.Rout', header=None)
    end_str = '#--- END ---------------------------------------------------------------------------------------------------'
    #cols_to_check = ["0"]
    
    src = r'C:/Users/jj/Desktop/autotranscribe'
    dest = r'C:/Users/jj/Desktop/Bulk_Wav'
    if __name__ == '__main__':
    
        #for col in cols_to_check:
            if not df[0].str.contains(end_str).any():
                #def copy(src, dest):
                    for file_path in glob.glob(os.path.join(src,'*.Rout'), recursive=True):
                        new_path = os.path.join(dest, os.path.basename(file_path))
                        shutil.copy(file_path, new_path)

My sample file factadwords.Rout contains -

> #--- END ---------------------------------------0-----------------------------------------------------------

So as you can see I have put 0 in there, which means the file should copy over to the destination folder since the file doesn't contain the exact end_str

The script runs successfully for one file, however, not all the files are copied over to the destination folder. Only one file factadwords.Rout get copied over even though other rout files in the folder dont have the valid end_str. why does this happen? how do i make it recursive to copy all files in the folder without a valid end_str over to the dest folder so the task is automated without me having to manually update df each time

print(file_path) Output from spyder call 'get_namespace_view': C:/Users/jj/Desktop/autotranscribe\factadwords.Rout

Upvotes: 1

Views: 188

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54945

Have you printed your dataframe at all? The first step in ANY debugging effort is to print all of your intermediate values to see what you are missing.

In this case, you seem to think your dataframe will have one long column. That's not true. Because you have blanks in there, your dataframe has three columns:

      0    1                                                  2
0  #---  END  ---------------------------------------0------...

So when you refer to df[0], that field does NOT have a '0' in it.

Upvotes: 1

Related Questions