Hướng dẫn how to create a file in a directory in python - cách tạo tệp trong thư mục trong python
Tôi đang gặp khó khăn khi tạo một thư mục và sau đó mở/tạo/ghi vào một tệp trong thư mục được chỉ định. Lý do có vẻ không rõ ràng với tôi. Tôi đang sử dụng os.mkdir () và Show
Tôi nhận được lỗi OSError: [Errno 2] No such file or directory: 'Some Path Name' Đường dẫn là hình thức 'tên thư mục có không gian không được tìm thấy' Tôi làm gì sai ở đây? Cập nhật: Tôi đã thử chạy mã mà không tạo thư mục
Vẫn gặp lỗi. Bối rối hơn nữa. Cập nhật 2: Vấn đề dường như là IMG_ALT, nó chứa '/' trong một số trường hợp, điều này gây ra rắc rối. Vì vậy, tôi cần xử lý '/'. Có cách nào để thoát khỏi '/' hoặc xóa là tùy chọn duy nhất không? Directory 'GeeksforGeeks' created Directory 'Geeks' created9 OSError: [Errno 2] No such file or directory: 'Some Path Name'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:
Ví dụ #3: Xử lý lỗi trong khi sử dụng phương thức Os.MakedIrs ().OSError: [Errno 2] No such file or directory: 'Some Path Name'6 OSError: [Errno 2] No such file or directory: 'Some Path Name'0
Ví dụ #1: Sử dụng phương thức OSError: [Errno 2] No such file or directory: 'Some Path Name'3 method to create directory/file OSError: [Errno 2] No such file or directory: 'Some Path Name'6 OSError: [Errno 2] No such file or directory: 'Some Path Name'0 OSError: [Errno 2] No such file or directory: 'Some Path Name'8 OSError: [Errno 2] No such file or directory: 'Some Path Name'9 0 1OSError: [Errno 2] No such file or directory: 'Some Path Name'9 3 4OSError: [Errno 2] No such file or directory: 'Some Path Name'9 6 7 8 9Directory 'GeeksforGeeks' created Directory 'Geeks' created0 Directory 'GeeksforGeeks' created Directory 'Geeks' created1 Directory 'GeeksforGeeks' created Directory 'Geeks' created2 OSError: [Errno 2] No such file or directory: 'Some Path Name'8 OSError: [Errno 2] No such file or directory: 'Some Path Name'9 Directory 'GeeksforGeeks' created Directory 'Geeks' created5 1OSError: [Errno 2] No such file or directory: 'Some Path Name'9 Directory 'GeeksforGeeks' created Directory 'Geeks' created8 Directory 'GeeksforGeeks' created Directory 'Geeks' created9 OSError: [Errno 2] No such file or directory: 'Some Path Name'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 4OSError: [Errno 2] No such file or directory: 'Some Path Name'9 6Traceback (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 8 9Directory 'GeeksforGeeks' created Directory 'Geeks' created0 Directory 'GeeksforGeeks' created Directory 'Geeks' created1 Directory 'GeeksforGeeks' created Directory 'Geeks' created2 Output: Directory 'GeeksforGeeks' created Directory 'Geeks' created
OSError: [Errno 2] No such file or directory: 'Some Path Name'3 method. 1OSError: [Errno 2] No such file or directory: 'Some Path Name'9 Directory 'GeeksforGeeks' created Directory 'Geeks' created8 Directory 'GeeksforGeeks' created Directory 'Geeks' created9 OSError: [Errno 2] No such file or directory: 'Some Path Name'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 1OSError: [Errno 2] No such file or directory: 'Some Path Name'9 3 4OSError: [Errno 2] No such file or directory: 'Some Path Name'9 6D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil3 8 9Directory 'GeeksforGeeks' created Directory 'Geeks' created0 Directory 'GeeksforGeeks' created Directory 'Geeks' created1 Directory 'GeeksforGeeks' created Directory 'Geeks' created2 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'
OSError: [Errno 2] No such file or directory: 'Some Path Name'3 method.
1OSError: [Errno 2] No such file or directory: 'Some Path Name'9 Directory 'GeeksforGeeks' created Directory 'Geeks' created8 Directory 'GeeksforGeeks' created Directory 'Geeks' created9 OSError: [Errno 2] No such file or directory: 'Some Path Name'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' created5 Directory 'Nikhil' created Directory 'c' created6 Directory 'Nikhil' created Directory 'c' created7 D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil3 Ví dụ #2: Lỗi trong khi sử dụng phương thức OSError: [Errno 2] No such file or directory: 'Some Path Name'3. Directory 'Nikhil' created Directory 'c' created7 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'
|