Created
January 20, 2017 13:21
-
-
Save somidad/0f7d7cd293a02d20653ec3b8a5a4b55e to your computer and use it in GitHub Desktop.
Rename URL encoded file name
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 | |
import urllib.parse as parse | |
import os | |
def print_usage(): | |
print('Usage: decodeurl.py encodedfilename') | |
def main(fname): | |
if not os.path.isabs(fname): | |
fname = os.path.abspath(fname) | |
directory, fname = os.path.split(fname) | |
if not os.path.lexists(directory + '/' + fname): | |
print('File does not exist') | |
exit() | |
decoded = parse.unquote(fname) | |
print(directory + '/' + fname) | |
print('will be renamed to') | |
print(directory + '/' + decoded) | |
k = input('Okay? [Y/n]') | |
if k == '' or k == 'y' or k == 'Y': | |
os.rename(directory + '/' + fname, | |
directory + '/' + decoded) | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) < 2: | |
print_usage() | |
exit() | |
main(sys.argv[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment