[Tutor] New to Python..Need help

Cameron Simpson cs at zip.com.au
Thu Sep 4 23:13:50 CEST 2014


On 04Sep2014 15:01, Joel Goldstick <joel.goldstick at gmail.com> wrote:
>On Thu, Sep 4, 2014 at 8:49 AM, Felisha Lawrence
><felisha.lawrence at gmail.com> wrote:
>> 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.
>>
>>
>Check out the documentation for os.walk and os.rename.  The first to
>collect you filenames, and the second to rename them.

os.listdir will be far easier than os.walk if it is a single flat directory.

Cheers,
Cameron Simpson <cs at zip.com.au>

Knox's box is a 286.                 Fox in Socks does hacks and tricks
Knox's box is hard to fix.           To fix poor Knox's box for kicks.
         - David Mar <mar at physics.su.oz.au>,
           as quoted by John Mackin <john at civil.su.oz.au>


More information about the Tutor mailing list