Skip to content

Instantly share code, notes, and snippets.

@srezasm
Created January 2, 2023 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srezasm/0e5e0692cab8f641313553f3d8d8f16f to your computer and use it in GitHub Desktop.
Save srezasm/0e5e0692cab8f641313553f3d8d8f16f to your computer and use it in GitHub Desktop.
move files into base directory python script
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment