The shutil
module lets you move files in Python. Here's the syntax:
# Preferably put this near the top of your script:
import shutil
# Use this to move a file:
shutil.move("path/to/original/file.csv", "path/to/destination/file.csv")
If all of your .csv files are currently in a directory and you want to generate a list of all their paths (basically the equivalent of "ls *.csv
" or "dir *.csv
"), you can use Python's os
module:
import os
my_csv_files = []
for file in os.listdir("/my_csv_dir"):
if file.endswith(".csv"):
my_csv_files.append(os.path.join("/my_csv_dir", file))
You could then iterate through the generated list. Or just replace the append
command with the aforementioned move
command to move each one to a new folder.
By the way, I see a lot of repeating code in your example. You may want to consider writing a couple of functions to take care of these tasks so that your code is easier to maintain and is 1/3 of the size.