Created
January 15, 2023 11:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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