Created
October 20, 2017 18:11
-
-
Save mvaldes14/18baa84193b7714e0ed52af5e98f64e5 to your computer and use it in GitHub Desktop.
Wallpaper_manager
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 python | |
# Wallpaper Manager | |
import os | |
import re | |
import random | |
PICTURE_PATH = '/home/yourusername/Pictures' | |
PICTURE_PATTERN = 'wall_\d*' | |
def get_files_not_matching(path, pattern): | |
""" Generates a list of files that do not follow the naming convention | |
Input parameters: | |
path = location of your picture folder | |
pattern = naming convention of the files you want to match | |
returns list of files not following convention and a list of all numbers already | |
used so you dont overwrite """ | |
picture_list = [] | |
availble_numbers = [] | |
for file in os.listdir(path): | |
wall_naming = re.search(pattern, file) | |
if wall_naming: | |
availble_numbers.append(file[5:7]) | |
else: | |
picture_list.append(file) | |
return picture_list, availble_numbers | |
def rename_files_not_matching(): | |
""" Renames files from list generated from function | |
Files are renamed based on the pattern and the current numbers Used | |
This also respects the extension """ | |
picture_list = get_files_not_matching(PICTURE_PATH, PICTURE_PATTERN)[0] | |
availble_numbers = get_files_not_matching(PICTURE_PATH, PICTURE_PATTERN)[1] | |
if picture_list: | |
print("Files to be renamed: ", picture_list) | |
for file in picture_list: | |
picture_number = random.randint(10, 100) | |
if picture_number not in availble_numbers: | |
if file.endswith('.png'): | |
os.rename(os.path.join(PICTURE_PATH, file), os.path.join( | |
PICTURE_PATH, "wall_" + str(picture_number) + '.png')) | |
elif file.endswith('.jpg'): | |
os.rename(os.path.join(PICTURE_PATH, file), os.path.join( | |
PICTURE_PATH, "wall_" + str(picture_number) + '.jpg')) | |
else: | |
pass | |
print("Files renamed") | |
else: | |
print("No files to be renamed") | |
def main(): | |
rename_files_not_matching() | |
# Run | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment