eolas/neuron/32f6269d-615a-4d39-8593-13ef9a9b8e03/File_operations_in_Python.md

34 lines
489 B
Markdown
Raw Normal View History

2024-10-25 09:51:23 +01:00
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
2024-10-25 13:28:55 +01:00
# File operations in Python
2024-10-25 09:51:23 +01:00
Most create, delete, move etc operations invoke the inbuilt `os` module.
// Add directory CRUD operations in Python
## Renaming files (moving)
```py
import os
os.rename('original-file-name.txt', 'new-file-name.txt')
```
## Deleting files
```py
import os
os.remove('file-name.txt')
```
2024-11-04 14:07:03 +00:00
## Check file exists
```python
import os
file_exists = os.exists('/file/path')
```