Sop*_*hie 23 io-redirection pycharm
使用命令行,如果我正在运行python文件,我可以输入:
python filename.py < filename.in > filename.out
Run Code Online (Sandbox Code Playgroud)
有没有办法模仿PyCharm中的这种行为?
yos*_*ico 27
(在Pycharm 5中添加)
在选项卡Edit Configurations
下的屏幕中,Logs
选中选项:
Save console output to file
并为输出文件提供FULL PATH.就是这样 - 就像魔法一样
您可以使用将内容加载到stdin StringIO
import StringIO
sys.stdin = StringIO.StringIO('your input')
Run Code Online (Sandbox Code Playgroud)
因此,如果要从文件重定向输入,则可以从文件读取数据并将其加载到stdin
import StringIO
input = "".join(open("input_file", "r").readlines())
sys.stdin = StringIO.StringIO(input)
Run Code Online (Sandbox Code Playgroud)
如果您想将filename作为第一个系统参数
import StringIO
filename = sys.argv[1]
input = "".join(open("input_file", "r").readlines())
sys.stdin = StringIO.StringIO(input)
Run Code Online (Sandbox Code Playgroud)
屏幕截图:
主程序
调试配置
与下面的示例代码类似的想法
import sys
sys.stdout = open(outputFile, mode='w', buffering=0)
#skip 'buffering' if you don't want the output to be flushed right away after written
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15976 次 |
最近记录: |