---
# System prepended metadata

title: Python - OS Module
tags: [Python]

---

```python
import os
```

## Basic Operations
- os.getcwd()
- os.listdir()
- os.makedirs()


```python
print("Current Directory: ", os.getcwd())
print(type(os.getcwd()))
```

    Current Directory:  C:\Users\Brian\Python\Learning Notes\Practices\OS
    <class 'str'>
    


```python
print("List all things in Current Diectory: ", os.listdir())
print(type(os.listdir()))
```

    List all things in Current Diectory:  ['.ipynb_checkpoints', 'os_files.ipynb', 'Untitled.ipynb']
    <class 'list'>
    


```python
# can only execute once. Will throw FileExistsError if execute over once.
os.makedirs("project") 
```


```python
# pass in the argument to ignore the FileExistsError
os.makedirs("project", exist_ok=True)
```


```python
print(os.path.exists("project"))
```

    True
    


## os.path


```python
# create a demo text file first
os.makedirs("project", exist_ok=True)
with open("project/test.txt", "w") as f:
    f.write("testing")
```


```python
print(os.path.isfile("project/test.txt")) # True
```

    True
    


```python
print(os.path.exists("project/test.txt")) # True
```

    True
    


```python
print(os.path.isdir("project/test.txt")) # False
```

    False
    


```python
os.makedirs("project/test", exist_ok=True)
print(os.path.isdir("project/test")) # True
```

    True
    

## Other useful methods


```python
# get the file name without the path
os.path.basename("project/test.txt")
```




    'test.txt'




```python
# get the name of the directory
os.path.dirname("project/test.txt")
```




    'project'




```python
# or just split the path name into two different variables
dir_name, filename = os.path.split("project/test.txt")
print(dir_name)
print(filename)
```

    project
    test.txt
    


```python
path = os.path.join(dir_name, filename)
print(path)
```

    project\test.txt
    

## Implement a file copy function


```python
import os
import shutil

def copy(path):
    # get directory name and base filename
    dir_name, filename = os.path.split(path)

    # create a new filename
    new_name = "copied_" + filename

    # concatenate a new filename with path
    new_path = os.path.join(dir_name, new_name)
    
    # copy file
    shutil.copy2(path, new_path)

    return os.path.isfile(new_path), new_path

```

```python
# test functionality
is_copied, filepath = copy("project/test.txt")

if is_copied:
    print(f"Successfully Copied to path: {filepath}")
else:
    print("Copy Failed")
```
```
Successfully Copied to path: project\copied_test.txt
```

```python
# copy using file i/o

def copy_file(path):
    dir_name, filename = os.path.split(path)
    new_name = "new_" + filename
    new_path = os.path.join(dir_name, new_name)

    with open(path, "r") as f:
        content = f.readlines()

    with open(new_path, "w") as f:
        f.writelines(content)

    return os.path.isfile(new_path), new_path

is_copied, filepath = copy_file("project/test.txt")

if is_copied:
    print(f"Successfully Copied to path: {filepath}")
else:
    print("Copy Failed")

```