Skip to content

Commit f61d2b4

Browse files
Merge pull request #28 from basnetsoyuj/master
Added 27-os-module.md
2 parents 73c7b71 + 8ab8d19 commit f61d2b4

File tree

2 files changed

+154
-1
lines changed

2 files changed

+154
-1
lines changed

26-file-handling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python File Handling
22

3-
**Video link:**
3+
**Video link:** [https://youtu.be/crluPcyuchU](https://youtu.be/crluPcyuchU)
44

55
In this video, we learned how to perform various file operations like reading and writing into files with the help examples.
66

27-os-module.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Python `os` module
2+
3+
**Video link:**
4+
5+
In this video, we learned to use the `os` module to perform various directory operations like creating, renaming, and removing directories with the help of examples.
6+
7+
**Programs in the Video**
8+
9+
- [Getting Current Directory](#getting-current-directory)
10+
- [Changing Directory](#changing-directory)
11+
- [Listing all Directories and Files](#listing-all-directories-and-files)
12+
- [Making a New Directory](#making-a-new-directory)
13+
- [Renaming a Directory or a File](#renaming-a-directory-or-a-file)
14+
- [Removing Directory or File](#removing-directory-or-file)
15+
16+
---
17+
18+
## Getting Current Directory
19+
We use the `getcwd()` function of the `os` module to get the path to the current directory.
20+
21+
```python
22+
import os
23+
24+
current_dir = os.getcwd()
25+
print(current_dir)
26+
```
27+
28+
When you run the code, the current working directory that is the directory containing our Python file is printed.
29+
30+
---
31+
32+
## Changing Directory
33+
34+
In Python, we can change the current working directory by using the `chdir()` function of the `os` module.
35+
36+
As we saw previously, the present working directory is the directory containing our Python file. Let's change the current working directory
37+
38+
```python
39+
import os
40+
41+
current_dir = os.getcwd()
42+
print(current_dir)
43+
44+
os.chdir("/path/new/location")
45+
print(os.getcwd())
46+
```
47+
48+
**Output**
49+
```
50+
<your file directory location>
51+
<new location>
52+
```
53+
54+
>**Note**: Now if we create a file inside the current directory, our file will be created inside `<new location>`.
55+
56+
```python
57+
import os
58+
59+
current_dir = os.getcwd()
60+
print(current_dir)
61+
62+
os.chdir("<new location>")
63+
64+
with open("test.txt", "w") as f:
65+
f.write("This is a test file.")
66+
```
67+
68+
We can see the `test.txt` file is created inside the current working directory which is `<new location>`.
69+
70+
---
71+
72+
## Listing all Directories and Files
73+
74+
In Python, all files and sub-directories inside a directory can be retrieved using the `listdir()` function of the `os` module.
75+
76+
For example,
77+
78+
```python
79+
import os
80+
81+
print(os.listdir())
82+
```
83+
84+
This function returns a list containing all files and sub-directories in the current working directory.
85+
86+
We can also pass an optional `path` argument to `listdir()` to return files and sub-directories from a specific path.
87+
88+
```python
89+
import os
90+
91+
print(os.listdir("<path>"))
92+
```
93+
94+
---
95+
96+
## Making a New Directory
97+
98+
We can create a new directory using the `mkdir()` function of the `os` module.
99+
100+
```python
101+
import os
102+
103+
os.mkdir("test")
104+
```
105+
106+
This creates a new `test` directory in our current directory.
107+
108+
---
109+
110+
## Renaming a Directory or a File
111+
112+
We can rename any directory or file using the `os.rename()` function which takes in 2 arguments: `old name` and `new name`.
113+
114+
```python
115+
import os
116+
117+
# rename directory or file
118+
os.rename('<old_name>', '<new_name>')
119+
```
120+
121+
---
122+
123+
## Removing Directory or File
124+
We can remove a file using the `remove()` function of the `os` module.
125+
126+
```python
127+
import os
128+
129+
print(os.listdir())
130+
131+
os.remove("<filename>")
132+
133+
print(os.listdir())
134+
```
135+
136+
After running this code, the file is deleted, so the second `os.listdir()` will not list the file.
137+
138+
---
139+
140+
To remove a directory, we use the `rmdir()` function.
141+
142+
```python
143+
import os
144+
145+
print(os.listdir())
146+
147+
os.rmdir("<path_to_directory>")
148+
149+
print(os.listdir())
150+
```
151+
152+
>**Note**: One thing we need to remember when removing a directory is that the directory must be empty. Otherwise, an exception will be raised.
153+

0 commit comments

Comments
 (0)