Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import os
  2. from glob import glob
  3. from os import rename, listdir
  4.  
  5. # Makes a .csv file with a row of file names per subdirectory
  6. # Appends "directoryName_" to each file
  7.  
  8. # Get the audio directory path
  9. path = os.path.dirname(os.path.realpath(__file__))
  10. folderName = os.path.basename(path)
  11.  
  12. csvFile = open('fileNamesPerDirectory.csv', 'w')
  13.  
  14. for root, directories, meh in os.walk(path):
  15. for directory in directories:
  16. files = listdir(os.path.join(path, directory))
  17.  
  18. namesNoExtension = []
  19.  
  20. for name in files:
  21. namesNoExtension.append(name.replace('.wav', ''))
  22.  
  23. # Write a row with file names per directory (to the CSV file)
  24. commaSeparatedString = ','.join(map(str, namesNoExtension))
  25. csvFile.write(directory + ',' + commaSeparatedString + '\n')
  26.  
  27. for file in files:
  28. if file.endswith('.wav'):
  29. pathToFile = os.path.join(root, directory, file)
  30.  
  31. newName = directory + '_' + file
  32. newPath = os.path.join(root, directory, newName)
  33.  
  34. rename(pathToFile, newPath)
  35.  
  36. csvFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement