【问题标题】:Subprocess check_output not working with SetFile (Mac)子进程 check_output 不适用于 SetFile (Mac)
【发布时间】:2018-06-04 19:13:41
【问题描述】:

我正在尝试使用 SetFile 更改 MacOS 文件的创建和修改日期 (see here)。

我的问题有点类似于 this one,但即使按照那里的建议,我也无法让它工作并得到错误 1 ​​作为回报(语法错误)

代码如下:

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))

如果我去掉 shell=True 我得到错误 2(其他错误),使用 shell 给出错误 1。

这是我得到的警告打印示例:

2018-06-04 21:06:06 ma​​in[84988] 警告错误命令 '['SetFile', '-d', '-m', "'01/13/2017 14:55:55'", "'/Volumes/Images/2017-01-12 Conference/2017-01-13 14.55.55.jpg'"]' 返回非零退出状态 2. on /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 Conference/2017-01-13 14.55.55.jpg'"]

知道我在哪里做错了吗?

谢谢

【问题讨论】:

    标签: python python-3.x macos subprocess


    【解决方案1】:

    好的,似乎 check_output 需要在调用之前添加引号。还必须为每个参数提供一个日期(-d 和 -m)

    这解决了问题:

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多