首页 > 解决方案 > 如何将终端输出从某个文件夹(ls 命令)保存到 txt 文件?对于 Pycharm

问题描述

我想将输出数据从终端保存到 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

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

我的错误是什么?

标签: pythonmacosterminal

解决方案


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

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


推荐阅读