# OS Module

## The OS Module

### To import:

```python
import os
```

### Get current working directory

```python
# pwd in linux
os.getcwd()
```

### Change directory

```python
# cd in linux
os.chdir("/Path/To/Wanted/Directory")
```

### List content of directory

```python
# ls in linux
os.listdir()
```

### Make a folder

```python
# mkdir in linux
os.mkdir("Name_of_folder_you_want")
```

* Can only be run once, as if the folder exists an error will be generated
  * `[Errno 17] File exists: folder_name`
* Adding a conditional to check if the folder exits:

```python
# os.listdir = lsdir in linux
if "myfolder" not in os.listdir("~/Desktop"):
	os.mkdir("myfolder")
```

### Making multiple directories

```python
# mkdir -p /path/to/folder in linux
os.makedirs("/path/to/folder")
```

* This can be run once for the same reasons as making a folder

### Remove directory

```python
# rmdir /path/to/directory in linux
os.rmdir("/path/to/directory")
```

* If the folder is not empty it will not delete it, same as Linux
* `os.rmdir` does not work on files, same as rmdir in Linux

**To remove in general, it is same as Linux**

```python
# rm /path/to/file in linux
os.remove("/path/to/file")
```

* Can be run only once if the file does not exits
* To prevent this, you can use the same `if` statement check in order to see if it exists

```python
# rm -rf /path/to/directory in linux
os.removedirs("/path/to/directory")
```

### Rename files

```python
# mv current_name new_name in linux
os.rename("current_name", "new_name")
```

## Traversing directories

```python
# Unpacking
for directory_path, dirnames, filenames in os.walk("/location/to/folder"):
	print(directory_path)
	print(dirnames)
	print(filenames)
```

* By "walking":
  * The first variable prints the `path to the file`
  * The second variable prints the `folders` inside the path provided
  * The third variable prints the `files` inside the path provided
* After which, if it finds a folder it goes on level deeper within the directory structure
* After which it starts the process again
* If multiple folders, it will take them separately and "walk" through them until the end

### Getting the environment variable

```python
# Getting the HOME variable
# echo $HOME in linux
os.environ.get("HOME")
```

### Join method

* Usually used when the path to folder is questionable
  * Such as does it contain a `/` at the end or not?

```python
# Concatinates basically ~/my_file.txt
os.path.join(os.environ.get("HOME"), "my_file.txt")
```

### Return just the filename

```python
# Will return just file.txt
print(os.path.basename("/path/to/file.txt"))
```

### Return just the directory

```python
# Will return just the "/path/to" folder structure, not the file
print(os.path.dirname("/path/to/file.txt"))
```

### Return both directory path and filename separately

```python
# Will return a tuple with directory and filename
print(os.path.split("/path/to/file.txt"))
# Example tuple: ("/path/to", "file.txt")
```

### Check if path exists

```python
# Boolean expression
os.path.exists("/path/to/file.txt")
# Returns True or Flase
```

* Usually used as a if statement

### Check if directory exits

```python
os.path.isdir("/path/to/file.txt")
```

### Check if file exists

```python
os.path.isfile("/path/to/file.txt")
```

### Exclude file format

```python
# Prints the entire path + extension in tuple
os.path.splitext("/path/to/file.txt")
# Example: ("/path/to/file", ".txt")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/courses/python-pcap-31-03-course/file-io-and-exception-handling/os-module.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
