【发布时间】:2018-02-09 16:08:07
【问题描述】:
cmd = 'touch -d '+date_in+' '+images_dir+'/'+photo_name
os.system(cmd)
没用
subprocess.call(['touch','-d','{}'.format(date_in),'{}'.format(images_dir+'/'+photo_name)])
没用
subprocess.Popen(['touch','-d','{}'.format(date_in),'{}'.format(images_dir+'/'+photo_name)])
有效!
为什么?在前两种情况下我缺少什么?
pi@raspberrypi:~ $ python --version
Python 2.7.13
实际代码sn-p:
try:
response = urllib2.urlopen(url)
if(response.getcode() == 200):
photo_file = response.read()
with open(images_dir+'/'+photo_name,'wb') as output:
output.write(photo_file)
#cmd = 'touch -d '+date_in+' '+images_dir+'/'+photo_name
#subprocess.Popen(['touch','-d','{}'.format(date_in),'{}'.format(images_dir+'/'+photo_name)])
subprocess.check_call(['touch','-d','{}'.format(date_in),'{}'.format(images_dir+'/'+photo_name)])
with open(images_dir+'/captions/'+photo_name+'.txt','wb') as output:
output.write(photo_title)
else:
print 'Download error'
except Exception as message:
print 'URL open exception {}'.format(message)
【问题讨论】:
-
“不起作用”到底是什么意思?如果有错误消息,您需要发布它。
-
没有错误信息。它悄悄地进入下一行代码
-
还将
call替换为check_call并报告结果。我很难相信这一点,没有冒犯 -
'{}'.format(images_dir+'/'+photo_name)呸! =>os.path.join(images_dir,photo_name) -
check_call就像call一样安静地完成,对文件日期没有影响。
标签: python subprocess system-calls