dongren9739 2017-08-31 19:42
浏览 279

如何使用GO中的“ os / exec”包将文件从一个目录复制到另一个目录

if I am in directory A and running the GO code, and I need to copy a file from directory B to directory C , how to do it? I tried adding cmd.Dir = "B" but it can copy the files in "B" directory, but when I try full path for directory "C" it throws error "exit status 1"

basic code sample

Currently in directory A with location "/var/A" cmd := exec.Command("cp","/var/C/c.txt","/var/B/") err := cmd.Run()

  • 写回答

1条回答 默认 最新

  • duanqiao2225 2017-09-01 01:49
    关注

    "os/exec" is the Go package used to run external programs, which would include Linux utilities.

    // The command name is the first arg, subsequent args are the
    // command arguments.
    cmd := exec.Command("tr", "a-z", "A-Z")
    // Provide an io.Reader to use as standard input (optional)
    cmd.Stdin = strings.NewReader("some input")
    // And a writer for standard output (also optional)
    var out bytes.Buffer
    cmd.Stdout = &out
    // Run the command and wait for it to finish (the are other
    // methods that allow you to launch without waiting.
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("in all caps: %q
    ", out.String())
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料