python - 如何将终端输出从某个文件夹(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
答:没有这样的文件或目录。
我的错误是什么?
解决方案
os.system(' ls /Users/user/Library/LaunchAgents/ > /PycharmProjects/Test_Project/File_new.txt')
首先是写命令的想法。它以这种方式对我有用。
推荐阅读
- c# - 发布到 api ASP.NET API 时收到错误 InvalidOperationException
- php - Laravel - 找不到页面 - 路由错误
- ios - 协议更新问题
- go - 并发示例混淆
- php - MySQL, PHP 通过比较多个表找到最大值/行
- java - 在增强的 for 循环的底层发生了什么?
- arrays - 如何为数组定义 JSON 模式?
- css - 可折叠表格行不超过单元格宽度
- reactjs - React - 如何编写将在智能组件内部访问的逻辑/服务类
- ruby-on-rails - 使用 Rails/React/TypeScript 将图像作为模块导入