简体   繁体   English

在显示控制台输入的同时,如何将输出写入文件?

[英]How to write the output to a file, while showing the inputs to the console?

If I want to print the results of a python script or an other program to a file, I simple do something like this on the console python myscript.py>output.txt, but now myscript has an input inside and how can I get this to the console? 如果我想将python脚本或其他程序的结果打印到文件中,我可以简单地在控制台python myscript.py> output.txt上执行类似的操作,但是myscript里面有一个输入,我该如何获取它到控制台? For example I have script to check if a given year is a leap year or not: 例如,我有脚本检查给定年份是否为a年:

def isLeapYear(year):
    return (year%4==0 and (year%400==0 or year%100!=0))

if __name__ == "__main__":
    y=input("Test if your year is a leap year: ")
    try:
        out=str(y)+" is"
        if not isLeapYear(int(y)):
              out+=" not";
        print(out+" a leap year")
    except ValueError:
        print("invaild input")

When I now write the command above, the result in my output-file is something like this (while I can just enter the year in the console): 现在,当我在上面写命令时,输出文件中的结果是这样的(虽然我可以在控制台中输入年份):

Test if your year is a leap year: 2000 is a leap year 测试您的年份是否为a年:2000年为a年

but I just want 但我只想

2000 is a leap year 2000年是a年

in there and have 在那里

Test if your year is a leap year: 测试您的年份是否为a年:

shown on the console. 显示在控制台上。 Is there a clever way to do this? 有聪明的方法吗? Can I somehow force print something to the console? 我可以以某种方式强制将某些内容打印到控制台吗? Is there a general way to this? 有一般的方法吗? I mean I could have the same problem with a C or Java-Program. 我的意思是我可能对C或Java程序有同样的问题。

According to the comments above: 根据以上评论:

*) You should handle the file writing in your python script, as we're in Python it's just a few lines of code. *)您应该使用python脚本来处理文件写入,因为我们在Python中只是几行代码。

*) About your question what would happen if someone still calls the script in the way that the output should be redirected, you can check in python if stdin and stdout are the same, if not the output got redirected according to Determining if stdout for a Python process is redirected *)关于您的问题,如果有人仍然以应重定向输出的方式调用脚本,将会发生什么情况,您可以在python中检查stdin和stdout是否相同,如果不是,则根据确定stdout是否为a进行重定向。 Python过程被重定向

To safe you one more click here's the example code, test.py is a two-liner that does exactly this check: 为了安全起见,请单击此处的示例代码,test.py是两行代码,它可以准确地执行此检查:

    $ cat test.py
    import os
    print os.fstat(0) == os.fstat(1)
    $ python test.py
    True
    $ python test.py > f
    $ cat f
    False
    $

Hope that helps. 希望能有所帮助。

Cheers, Peter 干杯,彼得

If you want to keep the terminal printing I'd just edit the script to write the output. 如果要保持终端打印,我只需编辑脚本以编写输出即可。

outfile = open("leap-output.txt", "a+")

Then add the writing to the script. 然后将文字添加到脚本中。

outfile.write("%s a leap year") %(out)
outfile.write("\n")
outfile.close()

Redirecting the output with > is a bash function and you don't have a control over that. >重定向输出是一个bash函数,您无法对此进行控制。

As said by zvone you gain some control if you force users to give arguments the script ie. 正如zvone所说,如果您强迫用户给脚本脚本参数,则您将获得一定的控制权。

userinput = sys.argv[1]

Then you'd start your script by typing python myscript.py example-output-file.txt 然后,您将通过键入python myscript.py example-output-file.txt启动脚本。

Python will crash here if user hasn't given the argument for the outfile. 如果用户未提供outfile的参数,Python将在此处崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM