Advertisement
Guest User

Untitled

a guest
Jan 30th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. # this just changes whether a female or male name list is generated
  2. female = True
  3.  
  4. # don't mess with stuff beyond this point unless you want to
  5.  
  6. import os
  7. from os.path import exists as file_exists
  8. os.chdir(os.getcwd())
  9.  
  10. # make sure possible names list exists, otherwise quit
  11. nameListExists = file_exists('possible names.txt')
  12. if nameListExists != True:
  13. print("Make sure to create a 'possible names.txt' file in this directory!")
  14. print("No changes made.")
  15. exit()
  16.  
  17. # adds a new line to the end of possible names to prevent issues later
  18. f = open("possible names.txt", mode = "a")
  19. f.write("\n")
  20. f.close
  21.  
  22. # overwrite existing name file if it exists, because we're appending later
  23. if female:
  24. f = open("female names.txt", mode = "w")
  25. else:
  26. f = open("male names.txt", mode = "w")
  27. f.write("")
  28. f.close
  29.  
  30. # this variable is used to count files and get names from the possible names list
  31. FileNum = -1
  32.  
  33. dirs = os.listdir(os.getcwd()) # get current directory for file list, possibly unneeded?
  34.  
  35. for file in dirs:
  36.  
  37. #each step is a new file and a new name
  38. FileNum = FileNum + 1
  39.  
  40. # get name from list
  41. PossibleNames = open("possible names.txt", "r")
  42. for i, line in enumerate(PossibleNames):
  43. if i == FileNum:
  44. line = line
  45. break
  46. PossibleNames.close()
  47.  
  48. # set variables
  49. newName = str(line)
  50. newName2 = str(line)
  51. currentName = str(file)
  52.  
  53. # separate file type from file name
  54. split_tup = os.path.splitext(currentName) # this line sometimes hands me trash, and i don't know why
  55. file_extension = split_tup[1]
  56.  
  57. # newName = newName + file_extension
  58. newName+=str(file_extension)
  59. newName = newName.replace("\n", "")
  60.  
  61. # prevents an error if their are more files than names, or vice versa
  62. if newName2 == "\n" or newName2 == "" or currentName == "":
  63. print("Out of names!")
  64. exit()
  65.  
  66. # save changes
  67. if ".txt" not in currentName and ".py" not in currentName: # save changes to files
  68. print(currentName + " changes to " + newName)
  69. os.rename(currentName, str(newName))
  70.  
  71. # for some reason, it's appending names that don't exist. this fixes that.
  72. nameListExists2 = file_exists(str(newName))
  73. if nameListExists2 == True:
  74. if female:
  75. f = open("female names.txt", mode = "a")
  76. f.write(newName2)
  77. f.close
  78. else:
  79. f = open("male names.txt", mode = "a")
  80. f.write(newName2)
  81. f.close
  82. else:
  83. #this isn't an image, don't waste a name
  84. FileNum = FileNum - 1
  85.  
  86. exit()
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement