English subtitles

← Renaming Files - Programming Foundations with Python

Get Embed Code
7 Languages

Showing Revision 4 created 05/25/2016 by Udacity Robot.

  1. So, I know I want to rename all of my files. And I remember there were about
  2. 50 photos, inside that folder that you downloaded. So,
  3. to rename 50 photos, I think I'll have to
  4. use some sort of a loop. So, I'll
  5. just write one down now by saying, for file_name,
  6. in file_list. Now, bear in mind, file_list is something
  7. we've seen before. In fact, we printed it out.
  8. This was a list of all of the photos inside
  9. the folder. And what this for loop is going to
  10. allow us to do is work with each photo file
  11. one at a time. So, for each photo, I want to change
  12. its name. And the old name is in file_name. And
  13. the new name, well, I know the new name is devoid
  14. of any numbers. But how to do that, I still
  15. don't know. And this means I'll have to do some experimentation.
  16. And to run those experiments I generally use the
  17. Python Shell window, where I can just start typing
  18. and seeing the result really quickly. Let me make
  19. more room for this window so you can see
  20. it properly. So, say, my file name was the
  21. following string; 48athens.jpg. Let me print that out, okay,
  22. good. Now, previously I Googled to find out that
  23. there is a string function called translate that takes
  24. up to two arguments. The first argument is a
  25. table. Which translates one set of characters to another set
  26. of characters and since we don't have that, I can
  27. use the keyword none. And the second is a list
  28. of all characters that we want to remove from
  29. the string. And those I know are numbers, so that
  30. will be zero, one, two, three, four, five, six, seven,
  31. eight, nine. All of those numbers. Let me try that.
  32. Oh hey, look, the file name now has
  33. no numbers in it. So, because that experiment
  34. worked, I will go back to the code
  35. and add this file_ name.translate function into my code.