MCPcopy Index your code
hub / github.com/geekcomputers/Python / add_to_dir

Function add_to_dir

Downloaded Files Organizer/move_to_directory.py:24–48  ·  view source on GitHub ↗
(ex, src_path, path)

Source from the content-addressed store, hash-verified

22
23
24def 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
51def rename(search, ex, dest_path):

Callers 1

on_createdMethod · 0.90

Calls 2

renameFunction · 0.85
moveMethod · 0.45

Tested by

no test coverage detected