What have you tried already? Which parts are working and which parts aren't?
This problem can be broken down into a few parts:
- Collect all file names
- Match substrings in file names
- Move files
Here's the pseudocode for what you're trying to do:
# There are fancier ways to accomplish this, but I
# think this is the easiest to understand.
names_list = get_file_names("in_this_folder")
for files in names_list:
if file_name contains "string1":
move(file_name,"to_this_folder")
elif file_name contains "string2":
move(file_name,"to_other_folder")
else:
do nothing?
To collect filenames in a directory, you might do something like this
import os
from pathlib import Path
import shutil # Notice! We're importing this for later in the answer.
# Define folder structure
basepath = Path().resolve()
zip_folder = basepath.joinpath("zip/to-be-processed")
destination_folder_1 = basepath.joinpath("zip/done/01")
destination_folder_2 = basepath.joinpath("zip/done/02")
# Create destination folders if they don't already exist
Path(destination_folder_1).mkdir(parents=True, exist_ok=True)
Path(destination_folder_2).mkdir(parents=True, exist_ok=True)
def get_file_names(path: Path, extension: str):
# Returns file names in path that match "*" + "extension"
names = os.listdir(path)
file_names = []
for i in range(len(names)):
# Look for the extension, case-insensitive.
if names[i].lower().endswith(extension.lower()):
file_names.append(names[i])
return file_names
if __name__ == "__main__":
# Create a list of the file names that looks something like this:
# ["file1.zip", "file2.zip", "etc..."]
file_names = get_file_names(zip_folder,"zip")
# Now move on to processing the list...
Now you have a list of all zip file names in the directory. Next, you'll find the substrings that you're interested in. You can use the .find()
string method for this. If the string that you're looking for exists in the string that you're searching, then find()
will return the position. If the string isn't found, then it returns -1.
Note that it's sometimes good idea to use .lower()
or .upper()
on both your candidate string and the substring that you're looking for (so you can find ".zip" and ".ZIP" files, for example).
>>> foo = "12345788_CATPICC1_2022_01_10_08_21_31.zip"
>>> bar = "12345788_CAICC1_2022_01_10_08_21_31.zip"
>>> foo.lower().find("CATPICC1".lower())
9
>>> bar.lower().find("CATPICC1".lower())
-1
Example of use:
# Look for the substring "special" in each of the file names.
file_names = ["file_1_special.zip","file_2.zip"]
for name in file_names:
if name.find("special") > -1:
# Do something
print("Found the file.")
else:
# Do something else.
print("Where are you????")
Then use shutil.move()
to move a file.
# File name as a string, just like we created earlier.
file_name = "moveme.zip"
# Move it.
shutil.move(zip_folder.joinpath(file_name),destination_folder_2)
Note that I'm making assumptions about your directory structure and where the files live in relation to your script. You will have to modify accordingly. Please look up pathlib.Path.resolve()
to understand what I did.