Skip to content

How do I write a file? #575

@74th

Description

@74th

I want to read a command output and write a new file.
What is good way to do it?

from invoke import task,Context,Result

@task
def replace(c):
    c:Context
    r:Result = c.run("cat file")
    t = r.stdout.replace("hoge","fuga")
    c.run(f'echo "{t}" > new_file')
from invoke import task,Context,Result

@task
def replace(c):
    c:Context
    r:Result = c.run("cat file")
    t = r.stdout.replace("hoge","fuga")
    with open("new_file", "w" ) as f:
        f.write(t)

If I use fabric2, I can use fabric.transfer.Transfer.put(). But must write a temporary file at local.
I think it's not good way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions