Skip to content

Instantly share code, notes, and snippets.

@kujiy
Created October 17, 2021 01:58
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 kujiy/ff7cd0bb63049dfebf8e075ff224a9f6 to your computer and use it in GitHub Desktop.
Save kujiy/ff7cd0bb63049dfebf8e075ff224a9f6 to your computer and use it in GitHub Desktop.
Rename files in dirs with the dir names
"""
# rename files in dirs
current_dir
|- dir1
|- 001.jpg
|- 002.jpg
|- dir2
|- 001.jpg
|- 002.jpg
Convert to
current_dir
|- dir1-001.jpg
|- dir1-002.jpg
|- dir2-001.jpg
|- dir2-002.jpg
"""
import os
li = os.listdir(".")
for d in li:
print(d)
if os.path.isdir(d):
files = os.listdir(f"{d}")
for f in files:
print(f"{d}-{f}")
os.rename(f"{d}{f}", f"{d}-{f}")
@kujiy
Copy link
Author

kujiy commented Oct 17, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment