Python没有明显原因写入终端?

时间:2016-10-17 19:04:38

标签: python

我有一个相当简单的python(3.5.2)脚本,它从一个文件读取,如果该行符合某些条件,那么它将该行复制到另一个。原始文件看起来像这样(这是一行,更多跟随,格式相同但数字不同):

  

2.9133 1 1 157.6578 170.4160 0.7081 3044.911 351.998 152.778 -1451.315 162.109

我的剧本就像:

Installing setuptools, pip, wheel...^CTraceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 11, in <module>
done.
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 669, in main
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 2327, in <module>
    raise SystemExit(popen.wait())
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1384, in wait
    main()
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 711, in main
    symlink=options.symlink)
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 944, in create_environment
    download=download,
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 900, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 767, in call_subprocess
    line = stdout.readline()
KeyboardInterrupt
    pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 476, in _eintr_retry_call
    return func(*args)
KeyboardInterrupt

程序完成任务很好,我得到了所需的输出。然而,当从python终端窗口运行时,它使用80左右的整数(就像70-90一样)以一种随机的方式为我的终端发送垃圾邮件。

我不知道可能会导致这种不良输出的原因,但它会严重影响我的工作空间。我希望你们中的一些人可能有想法解决它。

1 个答案:

答案 0 :(得分:1)

当您对此类文件执行.write时,这是正常行为。它是写入文件的字节数。

>>> a = open("new.txt", 'w')
>>> a.write("hello")
5

可能有更优雅的解决方案,但您可以通过将变量设置为out.write

的输出来抑制此问题
foo = out.write(line)