Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this just changes whether a female or male name list is generated
- female = True
- # don't mess with stuff beyond this point unless you want to
- import os
- from os.path import exists as file_exists
- os.chdir(os.getcwd())
- # make sure possible names list exists, otherwise quit
- nameListExists = file_exists('possible names.txt')
- if nameListExists != True:
- print("Make sure to create a 'possible names.txt' file in this directory!")
- print("No changes made.")
- exit()
- # adds a new line to the end of possible names to prevent issues later
- f = open("possible names.txt", mode = "a")
- f.write("\n")
- f.close
- # overwrite existing name file if it exists, because we're appending later
- if female:
- f = open("female names.txt", mode = "w")
- else:
- f = open("male names.txt", mode = "w")
- f.write("")
- f.close
- # this variable is used to count files and get names from the possible names list
- FileNum = -1
- dirs = os.listdir(os.getcwd()) # get current directory for file list, possibly unneeded?
- for file in dirs:
- #each step is a new file and a new name
- FileNum = FileNum + 1
- # get name from list
- PossibleNames = open("possible names.txt", "r")
- for i, line in enumerate(PossibleNames):
- if i == FileNum:
- line = line
- break
- PossibleNames.close()
- # set variables
- newName = str(line)
- newName2 = str(line)
- currentName = str(file)
- # separate file type from file name
- split_tup = os.path.splitext(currentName) # this line sometimes hands me trash, and i don't know why
- file_extension = split_tup[1]
- # newName = newName + file_extension
- newName+=str(file_extension)
- newName = newName.replace("\n", "")
- # prevents an error if their are more files than names, or vice versa
- if newName2 == "\n" or newName2 == "" or currentName == "":
- print("Out of names!")
- exit()
- # save changes
- if ".txt" not in currentName and ".py" not in currentName: # save changes to files
- print(currentName + " changes to " + newName)
- os.rename(currentName, str(newName))
- # for some reason, it's appending names that don't exist. this fixes that.
- nameListExists2 = file_exists(str(newName))
- if nameListExists2 == True:
- if female:
- f = open("female names.txt", mode = "a")
- f.write(newName2)
- f.close
- else:
- f = open("male names.txt", mode = "a")
- f.write(newName2)
- f.close
- else:
- #this isn't an image, don't waste a name
- FileNum = FileNum - 1
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement