(ex, src_path, path)
| 22 | |
| 23 | |
| 24 | def add_to_dir(ex, src_path, path): |
| 25 | file_with_ex = os.path.basename(src_path) |
| 26 | file_without_ex = file_with_ex[: file_with_ex.find(ex) - 1] |
| 27 | for cat, extensions in ext.items(): |
| 28 | if ex in extensions: |
| 29 | os.chdir(path) |
| 30 | dest_path = path + "\\" + cat |
| 31 | if cat in os.listdir(): |
| 32 | try: |
| 33 | shutil.move(src_path, dest_path) |
| 34 | except shutil.Error: |
| 35 | renamed_file = rename(file_without_ex, ex, dest_path) |
| 36 | os.chdir(path) |
| 37 | os.rename(file_with_ex, renamed_file) |
| 38 | os.chdir(dest_path) |
| 39 | shutil.move(path + "\\" + renamed_file, dest_path) |
| 40 | else: |
| 41 | os.mkdir(cat) |
| 42 | |
| 43 | try: |
| 44 | shutil.move(src_path, dest_path) |
| 45 | except Exception as e: |
| 46 | print(e) |
| 47 | if os.path.exists(src_path): |
| 48 | os.unlink(src_path) |
| 49 | |
| 50 | |
| 51 | def rename(search, ex, dest_path): |
no test coverage detected