[英]Subprocess check_output not working with SetFile (Mac)
I am trying to use SetFile to change MacOS file creation and modification date ( see here ). 我正在尝试使用SetFile更改MacOS文件的创建和修改日期( 请参阅此处 )。
My problem is somewhat similar to this one but even by following the advice there I can't get it to work and get an error 1 in return (bad syntax) 我的问题与此类似,但即使按照那里的建议,我也无法使它正常工作并返回错误1(语法错误)
Here is the code: 这是代码:
file_t = obj.decided_stamp.strftime('%m/%d/%Y %H:%M:%S')
# Use SetFile to change creation and modification date
cmd = ['SetFile', '-d', '-m', '{0}{1}{0}'.format('\'', file_t),
'{0}{1}{0}'.format('\'', obj.path)]
try:
check_output(cmd, shell=True)
except Exception as e:
logger.warning('Error {} on {} using {}'.format(e, obj.path, cmd))
If I take away the shell=True I get error 2 (other error), using shell gives be the error 1. 如果我拿走shell = True我得到错误2(其他错误),那么使用shell给出错误1。
Here is an example of warning print I get: 这是我得到的警告打印的示例:
2018-06-04 21:06:06 main [84988] WARNING Error Command '['SetFile', '-d', '-m', "'01/13/2017 14:55:55'", "'/Volumes/Images/2017-01-12 Conference/2017-01-13 14.55.55.jpg'"]' returned non-zero exit status 2. on /Volumes/Images/2017-01-12 Conference/2017-01-1314.55.55.jpg using ['SetFile', '-d', '-m', "'01/13/2017 14:55:55'", "'/Volumes/Images/2017-01-12 Conference/2017-01-13 14.55.55.jpg'"]
2018-06-04 21:06:06 main [84988]警告错误命令'['SetFile','-d','-m',“ '01 / 13/2017 14:55:55'”,“' / Volumes / Images / 2017-01-12 Conference / 2017-01-13 14.55.55.jpg'“]'返回了非零退出状态2.在/ Volumes / Images / 2017-01-12 Conference / 2017-01 -1314.55.55.jpg使用['SetFile','-d','-m',“'01/13/2017 14:55:55'”,“'/ Volumes / Images / 2017-01-12会议/ 2017-01-13 14.55.55.jpg'“]
Any idea what I am doing wrong there ? 知道我在那里做错了吗?
Thanks 谢谢
Ok, it seems that check_output required adding the quotes before the call. 好的,看来check_output需要在调用之前添加引号。 Also a date must be provided for each argument (-d and -m)
另外,必须为每个参数(-d和-m)提供一个日期
This solved the problem: 这样就解决了问题:
import shlex
file_t = obj.decided_stamp.strftime('\'%m/%d/%Y %H:%M:%S\'')
# Use SetFile to change creation and modification date
op = shlex.quote(obj.path)
cmd = ' '.join(['SetFile', '-d', file_t, '-m', file_t, op])
try:
check_output(cmd, shell=True)
except Exception as e:
logger.warning('Error {} on {} using {}'.format(e, obj.path, cmd))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.