-
-
Save idolpx/eadd12cb15daa61d9253f325a0381778 to your computer and use it in GitHub Desktop.
Move files to directories 0-z based on 1st char of filename
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/local/bin/python3 | |
# | |
# Move files to directories 0-z based on 1st char of filename | |
# https://gist.github.com/idolpx/eadd12cb15daa61d9253f325a0381778 | |
# | |
# Jaime Idolpx | |
# | |
import os, re, py7zr | |
#with py7zr.SevenZipFile('sample.7z', mode='r') as z: | |
# z.extractall() | |
(END) | |
#def extractCB(p1, p2): | |
# print( '[' + p1 + '] [' + p2 + ']' ) | |
# Getting the current work directory (cwd) | |
src_dir = os.getcwd() + os.sep | |
dest_dir = os.getcwd() + os.sep | |
def walk_through_files(path, file_extension=''): | |
for (dirpath, dirnames, filenames) in os.walk(path): | |
for filename in filenames: | |
if filename.endswith(file_extension): | |
yield [dirpath + os.sep, filename] | |
for path, file in walk_through_files ( src_dir ): | |
#print ( path + " + " + file ) | |
outdir = file[0].lower() + os.sep | |
# print ( outdir ) | |
if ( not os.path.exists( outdir ) ): | |
os.mkdir( outdir ) | |
# Clean up filename | |
outfile = file.lower() | |
outfile = re.sub(r' |\(.*\)|\'|,', '', outfile) | |
outfile = re.sub(r'\.prg$', '', outfile) | |
print ( path + file + " -> " + dest_dir + outdir + outfile ) | |
# Rename ISO file | |
os.rename( path + file, outdir + outfile ) | |
print( 'Done!' ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment