Skip to content

Instantly share code, notes, and snippets.

@i5ar
Last active August 29, 2015 14:10
Show Gist options
  • Save i5ar/d00be523d57c19506a99 to your computer and use it in GitHub Desktop.
Save i5ar/d00be523d57c19506a99 to your computer and use it in GitHub Desktop.
Organization
# Add file extension
import os
import glob
os.chdir("C:/Users/myFolder")
aList = []
for file in glob.glob("*"):
print(file)
# Add extension if does not exist
if '.' not in file:
fileEx = file + '.mp3'
aList.append(fileEx)
# Rename the files
oldName = file
newName = fileEx
#os.rename(oldName, newName)
print (aList)
# Print file names with in a specific extension
from __future__ import print_function
import glob
import os
os.chdir("C:/Users/myFolder")
for file in glob.glob("*.mp3"):
print(file)
# Change file names
from __future__ import print_function
import glob
import os
os.chdir("C:/Users/myFolder")
aList = []
for file in glob.glob("*.mp3"):
print(file)
aList.append(file)
print (aList)
# Add digits to list item and rename files
'''
number = 0
for file in aList:
number += 1
prevName = file
newName = str(number) + ' - ' + file
# Rename files
#os.rename(prevName, newName )
#print(newName)
'''
# Remove all digits from list items
''' First method '''
for s in aList:
result = ''.join([i for i in s[:-4] if not i.isdigit()])
print (result[1:])
''' Second method
for s in aList:
no_digits = []
# Iterate through the string adding non-numbers to the no_digits list
for i in s[:-4]: # Remove last 4 characters of the string
if not i.isdigit():
no_digits.append(i)
# Now join all elements of the list with '' which puts all of the characters together
result = ''.join(no_digits)
print (result)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment