Delisha_Majdobeh
Delisha_Majdobeh

Reputation: 3

cannot get shutil.move to move files

So I'm trying to move my csv files from the source folder to the dest folder after performing an action on each file using nested for loops

example of files in folder

Below are the nested for loops.

What's happening now is that the top file gets copied into the table in the database, but it doesn't get moved to destination folder after its contents are inserted into the sql table, and then the loop breaks after first run and prints the error in try block.

If I remove the shutil statement, all rows from each csv file successfully copies into database.

Essentially I want that to happen, but I also want to move each file, after I've copied all the data into the table, to the dest folder.

This script will be triggered on a power automate action that will run once a file is added to the folder. So I don't want to add/duplicate the rows in my database from the same file.

I'm also adding variables below this code so you can get an idea of what the function is doing as well.

Thanks for any help you can provide on this, and let me know if more clarification is needed.

My attempt:

for file in dir_list:

    source = r"C:\Users\username\source\{}".format(file)
    df = pd.read_csv(path2)
    df = df.dropna()
    rows= df_to_row_tuples(df)

    for row in rows:
        cursor.execute(sql, row)
        conn.commit() 

    shutil.move(source, destination)  

Variables:

def df_to_row_tuples(df):
    df = df.fillna('')
    rows = [tuple(cell) for cell in df.values]
    return rows

conn = sqlite3.connect(r'C:\Users\some.db')

cursor = conn.cursor()

sql = "INSERT INTO tblrandomtble VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
     
path = r'C:\Users\username\source'

dir_list = os.listdir(path)

source=""

destination= r"C:\Users\username\destination"

df = pd.DataFrame()

rows = tuple()

Upvotes: 0

Views: 261

Answers (1)

Auxilium
Auxilium

Reputation: 36

If the file already exists, the move function will overwrite it, provided you pass the whole path...including the file name

So add the file name to the destination arg of the shutil.move function...

Upvotes: 1

Related Questions