> I have a question regarding strings in python. I have a directory on my > MacBook Bro of about 13 files. I need to alter the file endings in that > directory. The files are on the order of > 'swp.113006004000_KLWX_0.0.5_PPI_v2','swp.113006004000_KLWX_0.0.5_PPI_v3'. I > need to remove the characters after the 'v' and replace with v20. All of the > endings of the files are sequential _v2, _v3,_v4, _v5. I need all of these > characters to be the same (i.e. v20). I would like to know which modules are > best to use, and how to use loops to alter them. Any help you can provide > would be great.
Hi Felisha, Do you have any prior programming experience? Your subject line suggests that you are new to Python. Are you familiar with any other programming? Give us more details, and we may be able to provide more appropriate advice. In lack of background information, we will assume for the moment that you have some basic programming skills, and will point to documentation where appropriate. We can point to: https://docs.python.org/2/tutorial/ to get a quick-and-dirty introduction to the language. For the operations you'll be doing, you probably want: 1. Some way to collect the set of file names. The glob module might be appropriate: https://docs.python.org/2/library/glob.html 2. Basic string manipulation skills to map the string: "swp.113006004000_KLWX_0.0.5_PPI_v2" to its replacement string: "swp.113006004000_KLWX_0.0.5_PPI_v20" For this particular pattern matching and string replacement, it might be enough to find the rightmost index for the substring "_v" using a string's rfind() method: https://docs.python.org/2/library/stdtypes.html#str.rfind string slicing (https://docs.python.org/2/tutorial/introduction.html#strings) to chop off the tail, and then a string append to put the replacement "_v20" at the end. For anything more sophisticated, you might want to investigate regular expressions. https://docs.python.org/2/howto/regex.html 3. Functions to interact with the operating system, to tell the operating system to rename a file from the old name to its replacement. Possibly os.rename(): https://docs.python.org/2/library/os.html#os.rename Please feel free to ask more questions. Good luck! _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor