How to rename the date stamp portion of a csv file in python
<p>I have created a script which should remove the date stamp portion from a file. For example, rename the file from existing name_2016-09-13.csv to name.csv. The problem is that the filename changes everyday. So, I need to rename and overwrite the existing file when it gets renamed the next day.</p>

<pre><code>import os
import re

path = "C:\New\Test"
for filename in os.listdir(path):
    if filename.startswith('name_'):
       print filename
       os.rename(filename, filename.translate("0123456789"))
</code></pre>

<p>If your question is how to grab the 'name' portion of the filename, here is a capturing regular expression that does the trick: <code>r'(\S+)_\d\d\d\d-\d\d-\d\d\.csv'</code>. Since you <code>import re</code> but then never use it, I imagine someone told you that you can accomplish your task using regular expressions, but you don't know how. I just gave you a hint, but you will probably need to read the docs for the re module in order to find out how to use the hint. Pay particular attention to the section on "capturing parentheses" and on the functions with names like find, search, match.</p>

python csv