_utils.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:treadmill 作者: Morgan-Stanley 项目源码 文件源码
def script_write(filename, script):
    """Write a script to a file.

    Proper execute permissions will be set.

    :param ``str`` filename:
        File to write to.
    :param ``script:
        String or iterable returning strings. Can be unicode.
    """
    if isinstance(script, six.string_types):
        # If the script is fully provided in a string, wrap it in a StringIO
        if hasattr(script, 'decode'):
            script = io.StringIO(script.decode())
        else:
            script = io.StringIO(script)

    with io.open(filename, 'wb') as f:
        for chunk in script:
            # The value must be properly encoded
            data = chunk.encode(encoding='utf8', errors='replace')
            f.write(data)
        if os.name == 'posix':
            os.fchmod(f.fileno(), 0o755)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号