Skip to content

Instantly share code, notes, and snippets.

@srezasm
Created January 2, 2023 16:34
Show Gist options
  • Select an option

  • Save srezasm/0e5e0692cab8f641313553f3d8d8f16f to your computer and use it in GitHub Desktop.

Select an option

Save srezasm/0e5e0692cab8f641313553f3d8d8f16f to your computer and use it in GitHub Desktop.

Revisions

  1. srezasm created this gist Jan 2, 2023.
    29 changes: 29 additions & 0 deletions moveFilesIntoBaseDir.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    from os import listdir, rename
    from os.path import isfile, isdir, join


    def scroll_dir(dpath):
    children = [c for c in listdir(dpath)]
    for c in children:
    p = join(dpath, c)
    if isdir(p):
    scroll_dir(p)
    elif isfile(p):
    change_file_name(p)


    def change_file_name(fpath):
    newname = str(fpath)\
    .replace(startpath, "")\
    .lstrip("/")\
    .replace("/", "__")
    newname = join(startpath, newname)

    rename(fpath, newname)
    print(f'{fpath} -> {newname}')


    if __name__ == "__main__":
    print("/base/dir/subdir/file.mp4 -> /base/dir__subdir__file.mp4")
    startpath = input("base dir path: ")
    scroll_dir(startpath)