# 80. Python's sys Module

**1. Accessing Command-Line Arguments**

This example shows how to retrieve command-line arguments using `sys.argv`.

```python
import sys

if __name__ == "__main__":
    print("Command-line arguments:")
    for arg in sys.argv:
        print(arg)
```

***

**2. Exiting a Program with sys.exit()**

You can use `sys.exit()` to exit a program gracefully, optionally providing an exit status.

```python
import sys

def main():
    print("This is an example of sys.exit()")
    sys.exit(0)

if __name__ == "__main__":
    main()
```

***

**3. Redirecting Output Using sys.stdout**

This example demonstrates redirecting standard output to a file.

```python
import sys

with open('output.txt', 'w') as f:
    sys.stdout = f
    print("This will be written to output.txt")
    
sys.stdout = sys.__stdout__  # Resetting stdout
```

***

**4. Checking the Python Version with sys.version**

You can check the version of Python being used with `sys.version`.

```python
import sys

print(f"Python version: {sys.version}")
```

***

**5. Working with sys.path for Importing Modules**

This example shows how to manipulate `sys.path` to control module search paths.

```python
import sys

# Print current sys.path
print("Before modifying sys.path:", sys.path)

# Add a new path
sys.path.append('/path/to/directory')

# Print updated sys.path
print("After modifying sys.path:", sys.path)
```

***

**6. Getting the Size of an Object with sys.getsizeof()**

This example shows how to use `sys.getsizeof()` to get the size of an object in bytes.

```python
import sys

a = [1, 2, 3]
size = sys.getsizeof(a)
print(f"Size of list 'a': {size} bytes")
```

***

**7. Handling Standard Error Output with sys.stderr**

This example shows how to write error messages to `sys.stderr`.

```python
import sys

print("This is a normal message.")
print("This is an error message.", file=sys.stderr)
```

***

**8. Getting the Recursion Limit with sys.getrecursionlimit()**

You can check and set the recursion limit using `sys.getrecursionlimit()` and `sys.setrecursionlimit()`.

```python
import sys

print(f"Current recursion limit: {sys.getrecursionlimit()}")
sys.setrecursionlimit(2000)
print(f"Updated recursion limit: {sys.getrecursionlimit()}")
```

***

**9. Using sys.stdin for User Input**

This example uses `sys.stdin` to read user input.

```python
import sys

print("Please enter your name:")
name = sys.stdin.readline().strip()
print(f"Hello, {name}!")
```

***

**10. Exiting with an Error Code Using sys.exit()**

This snippet shows how to exit a program with a specific error code.

```python
import sys

print("Something went wrong.")
sys.exit(1)  # Exit with error code 1
```

***

These examples demonstrate how to use the `sys` module for interacting with the interpreter, handling input/output, working with command-line arguments, and managing program behavior. The `sys` module is essential for low-level system interactions in Python.
