[Tutor] New to Python..Need help
Hello, 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. Thanks, Felisha Lawrence -- Felisha Lawrence Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student NASA URC/BCCSO Graduate Fellow NOAA NCAS Graduate Fellow Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer (240)-535-6665 (cell) felisha.lawre...@gmail.com (email) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python..Need help
On Thu, Sep 4, 2014 at 8:49 AM, Felisha Lawrence felisha.lawre...@gmail.com wrote: Hello, 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. Thanks, Felisha Lawrence -- Felisha Lawrence Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student NASA URC/BCCSO Graduate Fellow NOAA NCAS Graduate Fellow Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer (240)-535-6665 (cell) felisha.lawre...@gmail.com (email) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python..Need help
Is there anything different between the filenames aside from that suffix _vXX? If not, then you'll run into problems after the first filename is changed; further attempts won't allow the change, since there's already a file with that same name. AR On Thu, Sep 4, 2014 at 8:49 AM, Felisha Lawrence felisha.lawre...@gmail.com wrote: Hello, 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. Thanks, Felisha Lawrence -- Felisha Lawrence Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student NASA URC/BCCSO Graduate Fellow NOAA NCAS Graduate Fellow Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer (240)-535-6665 (cell) felisha.lawre...@gmail.com (email) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python..Need help
On 04/09/14 13:49, Felisha Lawrence wrote: '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). How else will you differentiate them if the files are all the same? Do you need to add a sequence number or do you really want to concateate the files into one big file? I would like to know which modules are best to use, and how to use loops to alter them. You can use os.listdir to get all the filenames in a single folder You can use glob.glob to get a subset of the files (using * and ? as wildvards) You can use os.walk to get all the files/folders from a directry tree Once you have the list of files you can use a for loop to access each name. You can the use string.replace() to make simple changes re.sub() to replace regular expressions Or you could use string slicing to replace certain characters based on position, wjhich might work well for your case. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python..Need help
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
Re: [Tutor] New to Python..Need help
On Thu, Sep 4, 2014 at 12:05 PM, taserian taser...@gmail.com wrote: Is there anything different between the filenames aside from that suffix _vXX? If not, then you'll run into problems after the first filename is changed; further attempts won't allow the change, since there's already a file with that same name. Ah. Nice catch! Yeah, that's a potential problem with the problem statement. Felisha, please clarify this point, because it's a big one. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python..Need help
On 04Sep2014 15:01, Joel Goldstick joel.goldst...@gmail.com wrote: On Thu, Sep 4, 2014 at 8:49 AM, Felisha Lawrence felisha.lawre...@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 c...@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 m...@physics.su.oz.au, as quoted by John Mackin j...@civil.su.oz.au ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python..Need help
These are all the files. No two filenames are the same swp.1120630020111.KLWX.0.0.5_PPI_v2 swp.1120630020143.KLWX.0.0.9_PPI_v3 swp.1120630020215.KLWX.0.1.3_PPI_v4 swp.1120630020247.KLWX.0.1.8_PPI_v5 swp.1120630020302.KLWX.0.2.4_PPI_v6 swp.1120630020316.KLWX.0.3.1_PPI_v7 swp.1120630020330.KLWX.0.4.0_PPI_v8 swp.1120630020344.KLWX.0.5.1_PPI_v9 swp.1120630020358.KLWX.0.6.4_PPI_v10 swp.1120630020411.KLWX.0.8.0_PPI_v11 swp.1120630020424.KLWX.0.10.0_PPI_v12 swp.1120630020437.KLWX.0.12.5_PPI_v13 swp.1120630020451.KLWX.0.15.6_PPI_v14 swp.1120630020504.KLWX.0.19.5_PPI_v15 On Thu, Sep 4, 2014 at 4:27 PM, Danny Yoo d...@hashcollision.org wrote: On Thu, Sep 4, 2014 at 12:05 PM, taserian taser...@gmail.com wrote: Is there anything different between the filenames aside from that suffix _vXX? If not, then you'll run into problems after the first filename is changed; further attempts won't allow the change, since there's already a file with that same name. Ah. Nice catch! Yeah, that's a potential problem with the problem statement. Felisha, please clarify this point, because it's a big one. -- Felisha Lawrence Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student NASA URC/BCCSO Graduate Fellow NOAA NCAS Graduate Fellow Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer (240)-535-6665 (cell) felisha.lawre...@gmail.com (email) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor