Skip to content

Instantly share code, notes, and snippets.

@srezasm
Created January 15, 2023 11:26
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/924beeafa572e6af80636b16fda27220 to your computer and use it in GitHub Desktop.
Save srezasm/924beeafa572e6af80636b16fda27220 to your computer and use it in GitHub Desktop.
Remove white space from all files in a directory and and sub-directories python script
from os import listdir, rename
from os.path import isfile, isdir, join, basename
def scroll_dir(dpath: str):
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: str):
name = basename(fpath)
newname = fpath.replace(name, name.replace(" ", ""))
rename(fpath, newname)
print(f'{fpath} -> {newname}')
if __name__ == "__main__":
print("/dir/File Name.txt -> /dir/FileName.txt")
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