sys

How do I get a line of the what the console returns (string) and place in a variable?

穿精又带淫゛_ 提交于 2019-12-11 09:20:34
问题 I have code that is using telnet and requires a login. If the login is incorrect, it returns "Incorrect login" to the console. I want to catch this exception and skip it so it doesn't stop the program. What I tried is below: try: session.write("username".encode('ascii') + b"\r") session.write("password".encode('ascii') + b"\r") ***this is the point where the console will return "Incorrect login"*** except sys.stdout == "Incorrect login": print(sys.stdout) pass else: **Rest of the code** It

Sys.path.insert inserts path to module but imports are not working

半腔热情 提交于 2019-12-11 08:24:05
问题 I want to import a module in a project and it gives me lots of troubles because of an import error. So I decided to write a little test to see where the problem lies. I add a folder to my sys path and try to import it. And I get an Import Error: no module found named xyz Like this: import sys import os sys.path.insert(0, os.path.abspath('../../myfolder')) import myfolder print(sys.path) The sys.path is ['/Users/myuser/myproject/mywebsitefolder/myfolder/', ...] myfolder contains an __init__.py

x86 assembly: printing integer to the console after mul (seg fault)

半世苍凉 提交于 2019-12-11 06:49:47
问题 I'm trying to learn x86 assembly. The book I'm using is Assembly Language - Step by Step, Programming With Linux (and I'd have to say it's pretty good). I've learned a lot so far, but I feel as though I should also be challenging myself to stay ahead in many respects so I can learn faster through doing (I can do follow along, top-down learning, but I find it tediously slow). So, I figured it would be a cool idea to try and multiply two registers (32-bit) and then output the data to the

Python: pygame.QUIT()

≯℡__Kan透↙ 提交于 2019-12-11 03:09:45
问题 Just been messing around with pygame and ran into this error. CODE: import sys import pygame pygame.init() size = width, height = 600, 400 screen = pygame.display.set_mode(size) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); ERROR: Traceback (most recent call last): File "C:/Users/Mike Stamets/Desktop/Mygame/Pygame practice/ScreenPractice.py", line 12, in <module> pygame.quit(); sys.exit(); SystemExit I was just trying to set a while loop so

Change OS for unit testing in Python

邮差的信 提交于 2019-12-11 02:19:52
问题 I have some python code that I've been tasked with unit testing that had different branches for different OS's. For example: if sys.platform == 'win32': #DoSomething if sys.platform == 'linux2': #DoSomethingElse I want to unit test both paths. Is there some way to temporarily change the sys.platform? Please let me know if I can provide any more information. 回答1: Well, you could simply do sys.platform = 'win32' , but it is quite ugly solution so instead try the mock module (it has been ported

pytest - ModuleNotFoundError - python 3.6.4

本小妞迷上赌 提交于 2019-12-10 17:54:22
问题 I have a project with this layout: ├── MANIFEST.in ├── README.md ├── __init__.py ├── company │ ├── __init__.py │ ├── api │ │ ├── __init__.py │ │ ├── auth.py │ │ ├── debug.py │ │ ├── exceptions.py │ │ ├── reporting.py │ │ ├── rest.py │ │ ├── soap.py │ │ └── utils.py │ ├── jinjaEnvironment.py │ ├── sql │ │ ├── __init__.py │ │ ├── connection.py │ │ └── sql_helper.py │ └── templates │ ├── __init__.py │ ├── getUser.xml │ ├── rest_write_custom_field.xml │ └── setUser.xml ├── company.egg-info ├──

Python 2.7 carriage return countdown

爱⌒轻易说出口 提交于 2019-12-10 14:33:16
问题 I'm having trouble implementing a simple countdown in python using a carriage return. I have two versions, each with problems. Print Version: for i in range(10): print "\rCountdown: %d" % i time.sleep(1) Problem: The \r doesn't do anything because a newline is printed at the end, so it gives the output: Countdown: 0 Countdown: 1 Countdown: 2 Countdown: 3 Countdown: 4 Countdown: 5 Countdown: 6 Countdown: 7 Countdown: 8 Countdown: 9 Sys.stdout.write Version: for i in range(10): sys.stdout.write

Python `print` passing extra text to sys.stdout?

浪子不回头ぞ 提交于 2019-12-10 14:09:06
问题 This is probably something stupid I am missing but it has really got me hung up on a larger project ( c extension) that I am writing. Why is print "Hello, World!" passing None and an extra \n to sys.stdout here? >>> import sys >>> class StdOutHook: ... def write(self, text): ... sys.__stdout__.write("stdout hook received text: %s\n" % repr(text)) ... >>> class StdErrHook: ... def write(self, text): ... sys.__stderr__.write("stderr hook received text: %s\n" % repr(text)) ... >>> sys.stdout =

Why doesn't sys.excepthook work?

蓝咒 提交于 2019-12-10 14:08:28
问题 Why isn't the sys.excepthook function called if I try to execute this code? import sys; def MyExcepthook(ex_cls, ex, tb): print("Oops! There's an Error.\n"); a=open("./ERR.txt","w"); #Fixed as suggested by unutbu BUT the problem is the same! a.write("Oops! There's an Error.\n"); a.close(); sys.excepthook = MyExcepthook; def main(): print(1/0); if (__name__=="__main__"): main(); Output: Traceback (most recent call last): File "C:\Users\Path\to\my\python\file.py", line 13, in <module> main();

Difference between os.close(0) & sys.stdin.close()

我怕爱的太早我们不能终老 提交于 2019-12-10 10:33:09
问题 I'm working on some Python code which is a CGI script called from Apache. The first thing the code does is (I believe) attempt to close stdin/stdout/stderr with the following: for fd in [0, 1, 2]: try: os.close(fd) except Exception: pass Normally this works, however if they're not open, I get a "python.exe has stopped working", "A problem caused the program to stop working correctly" error message (Win32 exception). Couple of questions: What's the difference between closing via os.close