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à

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
if not os.path.exists(path):
    os.mkdir(path)
temp_file=open(path+'/'+img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"

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

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"

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' created
9
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:

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

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

Cú pháp: os.mkdir (đường dẫn, mode = 0o777, *, dir_fd = none) os.mkdir(path, mode = 0o777, *, dir_fd = None)

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 biểu thị một đường dẫn.mode (tùy chọn): Giá trị số nguyên biểu thị chế độ của thư mục sẽ được tạo. Nếu tham số này bị bỏ qua thì giá trị mặc định OO777 được sử dụng.DIR_FD (Tùy chọn): Một mô tả tệp đề cập đến một thư mục. Giá trị mặc định của tham số này là không có. Nếu đường dẫn được chỉ định là tuyệt đối thì dir_fd bị bỏ qua.
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.

Lưu ý: Danh sách tham số ‘*trong danh sách tham số chỉ ra rằng tất cả các tham số sau (ở đây trong trường hợp của chúng tôi‘ DIR_FD,) là các tham số chỉ dành cho từ khóa và chúng có thể được cung cấp bằng tên của chúng, không phải là tham số vị trí. 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.

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

OSError: [Errno 2] No such file or directory: 'Some Path Name'
3 để tạo thư mục/tệp Use of
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
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
0

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
3

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
7

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

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' created
5

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

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

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

Output:

Directory 'GeeksforGeeks' created
Directory 'Geeks' created

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' created
5
Errors while using
OSError: [Errno 2] No such file or directory: 'Some Path Name'
3 method.

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
3

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

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

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

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'
8
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
5
Handling error while using
OSError: [Errno 2] No such file or directory: 'Some Path Name'
3 method.

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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' created
5
Directory 'Nikhil' created
Directory 'c' created
6

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

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' created
7
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
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'

OSError: [Errno 2] No such file or directory: 'Some Path Name'
6
[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'
3

OSError: [Errno 2] No such file or directory: 'Some Path Name'
8
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
[WinError 183] Cannot create a file when that file/
              /already exists: 'D:/Pycharm projects/GeeksForGeeks'
6
For example, consider the following path:

D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
2

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
8
os.makedirs(path, mode = 0o777, exist_ok = False)

Ví dụ #3: Lỗi xử lý trong khi sử dụng phương thức

OSError: [Errno 2] No such file or directory: 'Some Path Name'
3.
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

OSError: [Errno 2] No such file or directory: 'Some Path Name'
3 để tạo thư mục/tệp 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.

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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: Lỗi trong khi sử dụng phương thức

OSError: [Errno 2] No such file or directory: 'Some Path Name'
3.

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

Directory 'Nikhil' created successfully
9

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

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' created
5

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

OSError: [Errno 2] No such file or directory: 'Some Path Name'
17

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

Output:

Directory 'Nikhil' created
Directory 'c' created

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' created
5

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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: Lỗi trong khi sử dụng phương thức

OSError: [Errno 2] No such file or directory: 'Some Path Name'
3.

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

Directory 'Nikhil' created successfully
9

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

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'

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' created
5
Handling errors while using os.makedirs() method.

OSError: [Errno 2] No such file or directory: 'Some Path Name'
6
OSError: [Errno 2] No such file or directory: 'Some Path Name'
0

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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: Lỗi trong khi sử dụng phương thức

OSError: [Errno 2] No such file or directory: 'Some Path Name'
3.

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
4
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
6

Directory 'Nikhil' created
Directory 'c' created
5
OSError: [Errno 2] No such file or directory: 'Some Path Name'
52

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
8
path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
0
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
1
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
2

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' created
5

path=chap_name
print "Path : "+chap_path                       #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
1
OSError: [Errno 2] No such file or directory: 'Some Path Name'
9
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
8

Directory 'GeeksforGeeks' created
Directory 'Geeks' created
9
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

Output:

Directory 'Nikhil' created successfully

Làm thế nào để bạn tạo một tệp trong Python?

Sử dụng chức năng Mở (tên tệp FileName, W+W+) cho tệp văn bản tạo Python. for Python create text file.

Làm cách nào để lưu một tệp trong cùng một thư mục trong Python?

Lưu tệp lưu trong cùng một thư mục python của mã câu trả lời..
Nhập hệ điều hành.đường dẫn..
save_path = 'C:/ví dụ/'.
name_of_file = Raw_input ("Tên của tệp là gì:").
hoàn thành tên = hệ điều hành.đường dẫn.tham gia (save_path, name_of_file+". txt").

Làm cách nào để tạo một thư mục trong một thư mục trong Python?

Sử dụng hệ điều hành.Phương thức OS.MADEDIRS () trong Python được sử dụng để tạo một thư mục đệ quy.Điều đó có nghĩa là trong khi làm thư mục lá nếu thiếu bất kỳ thư mục cấp trung gian nào bị thiếu, hệ điều hành.Phương thức makedirs () sẽ tạo ra tất cả.os. makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os. makedirs() method will create them all.

Là một tập tin trong thư mục python?

Trong Python, bạn có thể kiểm tra xem một số tệp hoặc thư mục có tồn tại bằng các phương thức ISFILE () và ISDIR () hay không.Tuy nhiên, nếu bạn sử dụng isfile () để kiểm tra xem một thư mục nhất định có tồn tại hay không, phương thức sẽ trả về sai.Tương tự như vậy, nếu bạn sử dụng nếu isdir () để kiểm tra xem một tệp nhất định có tồn tại hay không, phương thức trả về sai.isfile() and isdir() methods, respectively. However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.