For questions about the Python module named sys. This tag is also used for questions relating to the Sys namespace - the root namespace for the Microsoft AJAX Library, which contains all fundamental classes and base classes.
Questions tagged [sys]
1100 questions
5
votes
0 answers
'sys.excepthook' and multiprocessing
I'm trying to use a custom sys.excepthook with the multiprocessing library to handle exceptions on all threads. I know there's an outstanding bug with python that prevents this from working correctly with the Threading library, and testing shows…

Foxocube
- 710
- 9
- 32
5
votes
1 answer
What is the difference between sys.stdout.encoding, locale.getpreferredencoding(), and sys.getdefaultencoding()?
I am new to python and really confused about this encoding stuff. So far, I've read about the following types of "encoding":
import sys
import locale
print (sys.stdout.encoding)
print (locale.getpreferredencoding())
print…

user3685285
- 6,066
- 13
- 54
- 95
5
votes
2 answers
Change default behavior of uncaught exception handling in python
I went through "Logging uncaught exceptions in Python". And, I have tried this:
import web
import sys
app = web.application((
'/test', 'test'), globals())
def test_func(e_type, value, traceback):
print "Handled exception here.."
class test:
…

Aashish P
- 1,894
- 5
- 22
- 36
5
votes
2 answers
How to pass sys.argv[n] into a function in Python
I am trying to pass "sys.argv[1]" into a function.
#!/usr/bin/env/ python
import sys
def main():
test(sys.argv[1])
def test(sys.argv[1]):
print "Hello " + sys.argv[1]
./arg.py World
File "./arg.py", line 5
def test(sys.argv[1]):
…

Greg
- 247
- 1
- 4
- 14
5
votes
5 answers
Python 2.7 carriage return countdown
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…

Kvass
- 8,294
- 12
- 65
- 108
5
votes
4 answers
taking multiline input with sys.stdin
I have the following function:
def getInput():
# define buffer (list of lines)
buffer = []
run = True
while run:
# loop through each line of user input, adding it to buffer
for line in sys.stdin.readlines():
…

Jakemmarsh
- 4,601
- 12
- 43
- 70
4
votes
2 answers
How to continuously read from stdin (not just once input file is done)?
I have these two scripts:
clock.py
#!/usr/bin/env python3
import time
while True:
print("True", flush=True)
time.sleep(1)
continuous_wc.py
#!/usr/bin/env python3
import sys
def main():
count = 0
for line in sys.stdin:
…

BigBoy1337
- 4,735
- 16
- 70
- 138
4
votes
2 answers
Passing Multiple String Arguments to Python Script
I have a python script where I need to pass multiple arguments. The first argument may have multiple emails, second is a string and the third is a string too.
Lets say I have the file
mytest.py:
def tfunc(em, st1, st2):
…

Stan
- 786
- 1
- 9
- 25
4
votes
3 answers
Subscribe to a file in /sys
Inotify won't trigger on file-changes in /sys - what ways are there to subscribe to changes in there?

Reactormonk
- 21,472
- 14
- 74
- 123
4
votes
0 answers
pytest: How to disable stdout capturing for only particular lines of test code
I am aware that one can disable stdout and stderr capturing in pytest by using the -s / --capture=no switch. Alternatively, one can use the capsys fixture and its disable method when testing a particular function:
def…

Florent H
- 323
- 1
- 8
4
votes
0 answers
Difference in sys.getsizeof, nbytes, and DataArray.size in xarray
I'm curious about the differences of some methods of checking how big an xarray DataSet is as I decide whether or not I can successfully load it all into memory.
I open up a set of precipitation netcdfs with:
imerg_xds =…

clifgray
- 4,313
- 11
- 67
- 116
4
votes
3 answers
How can I skip sys.argv[0] when dealing with arguments?
I have written a little Python script that checks for arguments and prints them if enough arguments or warnings if not enough or no arguments. But by default, the script itself is one of the arguments and I do not want to include it in my code…

K4R1
- 745
- 1
- 10
- 20
4
votes
2 answers
sys.argv[0] always returns nothing
I was trying to use sys.argv[0] to get the name of the script but it returned nothing. I guess that was because no script name was passed to the Python interpreter but I had no idea how to fix it.
my code:
import sys
print ("This is the name of the…

Xiaoyu
- 51
- 5
4
votes
1 answer
How to "continue" OR "exit" the program by pressing keys
Lets say I have 2 lines of code like this:
a = [1 , 2 , 3 , 4]
print (a)
Now I want to add something to this code to give the user 2 options:
1: press "Enter" to continue.
In this case the code should print "a"
2: press "Esc" to exit the program.…

Leo
- 479
- 2
- 6
- 16
4
votes
2 answers
Python change working direcory does not work properly?
Im new to python and are trying to figure out this for hours.. I want to change the working directory with os in my script by using
os.chdir("~") # not working.
os.getcwd #--> "/home/pi/Documents"
#I want to change into a subfolder I tried…

Felix
- 83
- 1
- 8