【问题标题】:How can I save terminale output from certain folder (ls command) to txt file? For Pycharm如何将终端输出从某个文件夹(ls 命令)保存到 txt 文件?对于 Pycharm
【发布时间】:2020-05-16 12:22:31
【问题描述】:

我想将终端的输出数据保存到 txt 文件。 我的代码在这里:

def file_exist():
    os.system('/Users/user/Library/LaunchAgents/ls > ~/PycharmProjects/Test_Project/File_new.txt')
    time.sleep(3)
    f = open ('File_new.txt', 'r')
    x = f.read()
    y = str(x)
    print (y)
    return y

答案:没有这样的文件或目录。

我的错误是什么?

【问题讨论】:

  • /Users/user您的计算机中的用户名是?
  • 是的,是测试用户。
  • 好的,你正在从当前目录打开 File_new,它保存在 pycharm 中。我是对的吗?这样做会发生什么? f = open ('~/PycharmProjects/Test_Project/File_new.txt', 'r')
  • 同样的答案,我无法执行ls命令的问题。
  • 你为什么用/Users/user/Library/LaunchAgents/ls而不是ls

标签: python macos terminal


【解决方案1】:

os.system('ls /Users/user/Library/LaunchAgents/ > /PycharmProjects/Test_Project/File_new.txt')

最初是写命令的想法。它以这种方式对我有用。

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多