Error when converting string to date object from command line in Python
<p>When I run the following code, I don't see any problems:</p>

<pre><code>as_of_date = '10-16-17'
today = datetime.datetime.strptime(as_of_date, '%m-%d-%y').date()
type(today)
print(today)

Out:  datetime.date
Out:  2017-10-16
</code></pre>

<p>However, when I run a script named <code>GetStats.py</code> that takes several arguments from the command line, I get an error.</p>

<p>The command line consists of 5 arguments and looks as follows:</p>

<pre><code>python   GetStats.py   Client_A.cfg   30   -d   10-16-17
</code></pre>

<p>The last argument ("10-16-17") on the command line represents a date.</p>

<p>The code in the GetStats.py script is as follows:</p>

<pre><code>import sys
from datetime import datetime, timedelta, time, date

as_of_date = sys.argv[4]
today = datetime.datetime.strptime(as_of_date, '%m-%d-%y').date()

print (today)
</code></pre>

<p>The code doesn't hit the print statement and instead shows the following error:</p>

<pre><code>type 'exceptions.AttributeError'
</code></pre>

<p>Does anyone see the mistake when I try to pass that date from the command line?</p>

<p>Thanks!</p>

<p>The problem is with your import:</p>

<p><code>from datetime import datetime</code> and then you try to call <code>datatime.datetime.strptime</code></p>

<p>By doing this, you're trying to call the function <code>datetime.datetime.datetime.strptime</code> which doesn't exist.</p>

<p>Therefore, to fix this change <code>datetime.datetime</code> to simply <code>datetime</code> in your code.</p>

python datetime