May-03-2018, 06:59 AM
Hi everyone. I'm completely new to Python and I'm hoping to get a little direction with a simple script. I'm trying to get the script to do the following:
1. from the current working directory, gather a list of all the subfolders and store them in variable
2. cd into each subfolder and create an additional set of folders (ie. cd Folder1 then mkdir Folder A, Folder B, Folder C)
3. while in the subfolder (Folder 1), move all the files that start with a certain string (ie. blaster*) into Folder A. Then move all the files that start with different string (ie. clash*) into Folder B, and so on.
I've written two versions of the script; One works if I feed it individual directory names by using sys.argv[1] (myscript.py Folder1) I'm trying to get the second script to run on every subdfolder in the current working directory. I hope I'm making sense and I apologize for sounding like a complete idiot.. Here's a copy of the script:
Kind Regards,
Shakir
1. from the current working directory, gather a list of all the subfolders and store them in variable
2. cd into each subfolder and create an additional set of folders (ie. cd Folder1 then mkdir Folder A, Folder B, Folder C)
3. while in the subfolder (Folder 1), move all the files that start with a certain string (ie. blaster*) into Folder A. Then move all the files that start with different string (ie. clash*) into Folder B, and so on.
I've written two versions of the script; One works if I feed it individual directory names by using sys.argv[1] (myscript.py Folder1) I'm trying to get the second script to run on every subdfolder in the current working directory. I hope I'm making sense and I apologize for sounding like a complete idiot.. Here's a copy of the script:
#!/usr/bin/python3 import os import shutil import sys import fnmatch top_dir = "/home/mmcneil/Desktop/TMP" root_dir = next(os.walk('.'))[1] work_dir = ['Blaster','Clash','Force','Lockup','PowerOFF','PowerON','Sign1','Spin','Stab','Swing'] for root_folder in root_dir: for folder in work_dir: os.makedirs(os.path.join(root_folder,folder), exist_ok=True) ## Everthing ABOVE this line works the way I want it to## ## Trying to get the script to chdir to 'top_dir/root_dir' for ret_folder in next(os.walk('.'))[1]: os.chdir(os.path.join(top_dir,ret_folder)) #for file in os.listdir('.'): for file in os.path.join(top_dir,ret_folder): dst = (work_dir) if fnmatch.fnmatch(file, 'blaster*'): shutil.move(file, dst[0]) if fnmatch.fnmatch(file, 'clash*'): shutil.move(file, dst[1]) if fnmatch.fnmatch(file, 'force*'): shutil.move(file, dst[2]) if fnmatch.fnmatch(file, 'lockup*'): shutil.move(file, dst[3]) if fnmatch.fnmatch(file, 'p*w*off*'): shutil.move(file, dst[4]) if fnmatch.fnmatch(file, 'p*w*on*'): shutil.move(file, dst[5]) if fnmatch.fnmatch(file, 'combo*'): shutil.move(file, dst[6]) if fnmatch.fnmatch(file, 'spin*'): shutil.move(file, dst[7]) if fnmatch.fnmatch(file, 'stab*'): shutil.move(file, dst[8]) if fnmatch.fnmatch(file, 'swing*'): shutil.move(file, dst[9])I appreciate any help I can get with this.
Kind Regards,
Shakir