擅长:python、mysql、java
<p><code>date</code>在其输出的末尾返回一个换行符,因此您需要使用<code>strip()</code>方法来删除它。在</p>
<pre><code>p = subprocess.Popen(['date +%m/%d/%y,%H:%M:%S'], stdout=subprocess.PIPE, shell=True)
stdout = p.communicate()[0].strip()
</code></pre>
^{pr2}$
<p>但是,如果您希望在文件名中添加日期/时间戳,最好直接在Python中使用<a href="https://docs.python.org/2/library/datetime.html" rel="nofollow">datetime module</a>和<a href="https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior" rel="nofollow">strftime</a>函数:</p>
<pre><code>from datetime import datetime
print datetime.now().strftime('%m/%d/%y,%H:%M:%S')
>>> '01/30/15,10:47:37'
</code></pre>