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 đề?
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
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'
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'
>>> import os.path
>>> os.path.join['first_part', 'second_part']
'first_part\\second_part'
>>> os.path.join['first_part', '/second_part']
'/second_part'
4In [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' created764current_directory = os.getcwd[] final_directory = os.path.join[current_directory, r'new_folder'] if not os.path.exists[final_directory]: os.makedirs[final_directory]
9In [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'
66current_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]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]
Directory 'Nikhil' created Directory 'c' created78>>> import os.path >>> os.path.join['first_part', 'second_part'] 'first_part\\second_part' >>> os.path.join['first_part', '/second_part'] '/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'
71current_directory = os.getcwd[] final_directory = os.path.join[current_directory, r'new_folder'] if not os.path.exists[final_directory]: os.makedirs[final_directory]
Directory 'GeeksforGeeks' created Directory 'Geeks' created1Directory 'GeeksforGeeks' created Directory 'Geeks' created2
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' created975 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.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]
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
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>>> import os.path
>>> os.path.join['first_part', 'second_part']
'first_part\\second_part'
>>> os.path.join['first_part', '/second_part']
'/second_part'
79 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]
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'
1In [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]
20Directory 'GeeksforGeeks' created Directory 'Geeks' created9
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'
4In [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/Nikhil2
>>> 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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
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'
8In [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'
1In [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]
20Directory 'GeeksforGeeks' created Directory 'Geeks' created9
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'
4In [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/Nikhil2
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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
Output:
Directory 'GeeksforGeeks' created Directory 'Geeks' created
8In [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 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'
17 Errors while using 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]
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'
1In [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]
20Ví 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'
4In [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/Nikhil2
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil3
>>> 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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
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'
8In [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 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'
17 Handling error while using 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]
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'
1In [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]
20Directory 'Nikhil' created Directory 'c' created5
Directory 'Nikhil' created Directory 'c' created6
Directory 'Nikhil' created Directory 'c' created7
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil3
Directory 'GeeksforGeeks' created Directory 'Geeks' created9
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' created7
>>> import os.path
>>> os.path.join['first_part', 'second_part']
'first_part\\second_part'
>>> os.path.join['first_part', '/second_part']
'/second_part'
8Traceback [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'
0Cú 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'
8In [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'
1In [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'
4In [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/Nikhil2
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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
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'
8In [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'
1In [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]
20Directory 'GeeksforGeeks' created Directory 'Geeks' created9
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'
4In [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/Nikhil2
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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
Output:
Directory 'Nikhil' created Directory 'c' created
8In [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 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'
17current_directory = os.getcwd[]
final_directory = os.path.join[current_directory, r'new_folder']
if not os.path.exists[final_directory]:
os.makedirs[final_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'
8In [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'
1In [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'
4In [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/Nikhil2
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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
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'
8In [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 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'
17 Handling errors while using os.makedirs[] method.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]
>>> import os.path
>>> os.path.join['first_part', 'second_part']
'first_part\\second_part'
>>> os.path.join['first_part', '/second_part']
'/second_part'
1In [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]
20In [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'
8In [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'
1In [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'
4In [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/Nikhil2
Directory 'Nikhil' created Directory 'c' created5
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'
9Directory 'GeeksforGeeks' created Directory 'Geeks' created0
Directory 'GeeksforGeeks' created Directory 'Geeks' created1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil8
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'
8In [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'
1In [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]
20Directory 'GeeksforGeeks' created Directory 'Geeks' created9
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