Hướng dẫn how do you create a new directory in the current directory in python? - làm thế nào để bạn tạo một thư mục mới trong thư mục hiện tại trong python?

Tôi có một chương trình trong Python trong các quy trình, nó tạo ra một số tệp. Tôi muốn chương trình nhận ra thư mục hiện tại và sau đó tạo một thư mục bên trong thư mục, để các tệp được tạo sẽ được đưa vào thư mục đó.

Tôi đã thử điều này:

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'/new_folder')
if not os.path.exists(final_directory):
    os.makedirs(final_directory)

Nhưng nó không cho tôi những gì tôi muốn. Có vẻ như dòng thứ hai không hoạt động như tôi muốn. Có ai có thể giúp tôi giải quyết vấn đề?

Hướng dẫn how do you create a new directory in the current directory in python? - làm thế nào để bạn tạo một thư mục mới trong thư mục hiện tại trong python?

Rocketdonkey

Phim huy hiệu vàng 35,5K77 gold badges78 silver badges84 bronze badges

Đã hỏi ngày 2 tháng 1 năm 2013 lúc 16:45Jan 2, 2013 at 16:45

2

Hãy nghĩ rằng vấn đề là trong

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
1 và dấu gạch chéo (đề cập đến thư mục gốc) được sử dụng trong đó.

Hãy thử nó với:

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)

Cần làm việc.

Đã trả lời ngày 2 tháng 1 năm 2013 lúc 16:54Jan 2, 2013 at 16:54

Hetschhetschhetsch

1.4282 Huy hiệu vàng12 Huy hiệu bạc27 Huy hiệu đồng2 gold badges12 silver badges27 bronze badges

4

Một điều cần lưu ý là (theo tài liệu

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
2) nếu một đường dẫn tuyệt đối được cung cấp dưới dạng một trong những đối số, các yếu tố khác sẽ bị vứt đi. Ví dụ (trên Linux):

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'

Và trên Windows:

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'

Vì bạn bao gồm một

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
3 hàng đầu trong đối số
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
4 của bạn, nó đang được hiểu là một con đường tuyệt đối và do đó bỏ qua phần còn lại. Do đó, bạn nên loại bỏ
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
3 từ đầu đối số thứ hai để có biểu diễn tham gia như mong đợi. Lý do bạn không phải bao gồm
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
3 là vì
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
2 sử dụng ngầm
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
8, đảm bảo rằng bộ phân cách thích hợp được sử dụng (lưu ý sự khác biệt trong đầu ra ở trên cho
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
9).

Đã trả lời ngày 2 tháng 1 năm 2013 lúc 16:56Jan 2, 2013 at 16:56

RocketdonkeyrocketdonkeyRocketDonkey

Phim huy hiệu vàng 35,5K77 gold badges78 silver badges84 bronze badges

Ví dụ #2:

Ví dụ #3: Xử lý lỗi trong khi sử dụng phương thức Os.MakedIrs ().

  • os.mkdir()
  • os.makedirs()

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
6
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
0

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
6

Directory 'Nikhil' created
Directory 'c' created
7
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
64
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
66
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
67
os.mkdir(path, mode = 0o777, *, dir_fd = None)

Directory 'Nikhil' created
Directory 'c' created
7
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
71
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional): A Integer value representing mode of the directory to be created. If this parameter is omitted then default value Oo777 is used.
dir_fd (optional): A file descriptor referring to a directory. The default value of this parameter is None.
If the specified path is absolute then dir_fd is ignored.

Directory 'Nikhil' created
Directory 'c' created
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
75
The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter.

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào. This method does not return any value.

Directory 'Nikhil' created
Directory 'c' created
7
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
79
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2
Use of
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
3 method to create directory/file

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'
1

Ví dụ #2:

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
7

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'
1

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'
5

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

Output:

Directory 'GeeksforGeeks' created
Directory 'Geeks' created

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17
Errors while using
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
3 method.

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
6
[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'
3

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

Ví dụ #2:

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
3

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

Output:

Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17
Handling error while using
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
3 method.

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
6
[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'
3

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

Directory 'Nikhil' created
Directory 'c' created
5
Directory 'Nikhil' created
Directory 'c' created
6

Directory 'Nikhil' created
Directory 'c' created
7
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
3

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'
1

Directory 'Nikhil' created
Directory 'c' created
7
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
Traceback (most recent call last):
  File "gfg.py", line 22, in 
    os.makedirs(path)
  File "C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python/
       / \Python38-32\lib\os.py", line 221, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that/
               / file already exists: 'D:/Pycharm projects/GeeksForGeeks/Authors\\Nikhil'
3

Output:

[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'

Ví dụ #2:

Ví dụ #3: Xử lý lỗi trong khi sử dụng phương thức Os.MakedIrs ().
For example, consider the following path:

D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
6
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
0

Cú pháp: os.makedirs (đường dẫn, mode = 0O777, tồn tại_ok = false) os.makedirs(path, mode = 0o777, exist_ok = False)

Tham số: Đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Một đối tượng giống như đường dẫn là một chuỗi hoặc đối tượng byte đại diện cho đường dẫn.mode (tùy chọn): Giá trị số nguyên biểu thị chế độ của thư mục mới được tạo. Nếu tham số này bị bỏ qua thì giá trị mặc định OO777 được sử dụng.exist_ok (tùy chọn): Giá trị mặc định Sai được sử dụng cho tham số này. Nếu thư mục đích đã tồn tại, một oserror sẽ được nâng lên nếu giá trị của nó là sai nếu không.
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional): A Integer value representing mode of the newly created directory. If this parameter is omitted then the default value Oo777 is used.
exist_ok (optional): A default value False is used for this parameter. If the target directory already exists an OSError is raised if its value is False otherwise not.

Loại trả về: Phương thức này không trả về bất kỳ giá trị nào. This method does not return any value.

Ví dụ #1: Sử dụng phương thức

Traceback (most recent call last):
  File "gfg.py", line 22, in 
    os.makedirs(path)
  File "C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python/
       / \Python38-32\lib\os.py", line 221, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that/
               / file already exists: 'D:/Pycharm projects/GeeksForGeeks/Authors\\Nikhil'
4 để tạo thư mục. Use of
Traceback (most recent call last):
  File "gfg.py", line 22, in 
    os.makedirs(path)
  File "C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python/
       / \Python38-32\lib\os.py", line 221, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that/
               / file already exists: 'D:/Pycharm projects/GeeksForGeeks/Authors\\Nikhil'
4 method to create directory.

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
6
[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'
3

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
02

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
05

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
09

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'
1

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
27

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

Output:

Directory 'Nikhil' created
Directory 'c' created

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
6
[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'
3

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
02

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
05

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
09

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

Output:

Traceback (most recent call last):
  File "gfg.py", line 22, in 
    os.makedirs(path)
  File "C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python/
       / \Python38-32\lib\os.py", line 221, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that/
               / file already exists: 'D:/Pycharm projects/GeeksForGeeks/Authors\\Nikhil'

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17
Handling errors while using os.makedirs() method.

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
02

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
05

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
4
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

Directory 'Nikhil' created
Directory 'c' created
5
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
62

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
8
>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8

In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
8
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
17

>>> import os.path
>>> os.path.join('first_part', 'second_part')
'first_part\\second_part'
>>> os.path.join('first_part', '/second_part')
'/second_part'
1
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
20

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
In [1]: import os.path

In [2]: os.path.join('first_part', 'second_part')
Out[2]: 'first_part/second_part'

In [3]: os.path.join('first_part', r'/second_part')
Out[3]: '/second_part'
9
Traceback (most recent call last):
  File "gfg.py", line 18, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file /
                 /already exists: 'D:/Pycharm projects/GeeksForGeeks'
1

Output:

current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'new_folder')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
0