Last active
January 16, 2017 06:15
-
-
Save iamaamir/d227dc44cf1853c33edd9e7a80f9a6b9 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from os import listdir, chdir, rename | |
# Valid chars in a file name | |
alpha = 'abcdefghijklmnopqrstuvwxyz0123456789.' | |
dir_path = "/home/varun/Desktop/Github Projects/CodeKata/Python" | |
total_files = len(listdir(dir_path)) | |
for i in listdir(dir_path): | |
ans = '' | |
for j in i: | |
if j.lower() in alpha: | |
ans += j | |
else: | |
if j == ' ': | |
ans += '_' | |
chdir(dir_path) | |
rename(i, ans) | |
# Getting the final count of files in the folder after renaming | |
final_count = len(listdir(dir_path)) | |
# Final Verification | |
print('Number of files present initially: ', total_files) | |
print('Number of file renamed: ', final_count) | |
print('Difference: ', (total_files - final_count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment