https://github.com/zero1max/python-directory
This project provides examples and explanations on how to work with directories in Python. It covers operations such as getting the current directory, changing directories, joining and splitting paths, checking directory existence, creating, renaming, deleting directories, and traversing directories recursively.
https://github.com/zero1max/python-directory
os python3
Last synced: about 1 year ago
JSON representation
This project provides examples and explanations on how to work with directories in Python. It covers operations such as getting the current directory, changing directories, joining and splitting paths, checking directory existence, creating, renaming, deleting directories, and traversing directories recursively.
- Host: GitHub
- URL: https://github.com/zero1max/python-directory
- Owner: zero1max
- Created: 2025-02-12T05:24:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-12T05:57:32.000Z (over 1 year ago)
- Last Synced: 2025-02-12T06:49:49.135Z (over 1 year ago)
- Topics: os, python3
- Language: Python
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Python Directory Management
This repository provides examples and explanations on how to work with directories in Python.
## 📂 What You Will Learn
- Getting the current working directory
- Changing directories
- Joining and splitting paths
- Checking if a path is a directory
- Creating, renaming, and deleting directories
- Traversing directories recursively
## 🛠 Requirements
Make sure you have Python installed. You can check your Python version by running:
```sh
python --version
```
## 📌 Usage
Clone the repository and navigate to the project folder:
```sh
git clone https://github.com/yourusername/python-directory.git
cd python-directory
```
## 📜 Code Examples
### 1️⃣ Get the Current Directory
```python
import os
cwd = os.getcwd()
print(cwd) # Prints the current working directory
```
### 2️⃣ Change the Current Working Directory
```python
import os
os.chdir('/main2') # Change to /main2 directory
swd = os.getcwd()
print(swd)
```
### 3️⃣ Join and Split a Path
```python
import os
fp = os.path.join('temp', 'python')
print(fp) # temp\python (on Windows)
pc = os.path.split(fp)
print(pc) # ('temp', 'python')
```
### 4️⃣ Check If a Path Is a Directory
```python
import os
dir = os.path.join("C:\\", "temp")
print(dir)
if os.path.exists(dir) or os.path.isdir(dir):
print(f'The {dir} is a directory')
```
### 5️⃣ Create a Directory
```python
import os
dir = os.path.join("C:\\", "temp", "python")
if not os.path.exists(dir):
os.mkdir(dir)
```
### 6️⃣ Rename a Directory
```python
import os
oldpath = os.path.join("C:\\", "temp", "python")
newpath = os.path.join("C:\\", "temp", "python3")
if os.path.exists(oldpath) and not os.path.exists(newpath):
os.rename(oldpath, newpath)
print("'{0}' was renamed to '{1}'".format(oldpath, newpath))
```
### 7️⃣ Delete a Directory
```python
import os
dir = os.path.join("C:\\", "temp", "python")
if os.path.exists(dir):
os.rmdir(dir)
print(dir + ' is removed.')
```
### 8️⃣ Traverse a Directory Recursively
```python
import os
path = "c:\\temp"
for root, dirs, files in os.walk(path):
print("{0} has {1} files".format(root, len(files)))
```
## 🏁 Summary
- Use `os.getcwd()` to get the current working directory.
- Use `os.chdir()` to change the working directory.
- Use `os.mkdir()` to create a new directory.
- Use `os.rename()` to rename a directory.
- Use `os.rmdir()` to remove a directory.
- Use `os.walk()` to list the contents of a directory.
## 📜 License
This project is licensed under the MIT License.