Python OS modulis nodrošina funkcijas mijiedarbībai ar operētājsistēmu. OS ietilpst Python standarta utilīta moduļos. Šis modulis nodrošina pārnēsājamu veidu, kā izmantot no operētājsistēmas atkarīgu funkcionalitāti. Theos>unos.path>moduļi ietver daudzas funkcijas mijiedarbībai ar failu sistēmu. Visas OS moduļa funkcijas tiek paaugstinātasOSError>ja ir nederīgi vai nepieejami failu nosaukumi un ceļi, vai citi argumenti, kuriem ir pareizais tips, bet kurus operētājsistēma nepieņem.
OS modulī ir pieejamas dažādas direktora izveides metodes. Šie ir -
Izmantojot os.mkdir()
os.mkdir()>Python metode tiek izmantota, lai izveidotu direktoriju ar nosaukumu ceļš ar norādīto ciparu režīmu. Šī metode paaugstinaFileExistsError>ja izveidojamais direktorijs jau pastāv.
Sintakse: os.mkdir(ceļš, režīms = 0o777, *, dir_fd = nav)
Parametrs:
ceļš: Ceļam līdzīgs objekts, kas attēlo failu sistēmas ceļu. Ceļam līdzīgs objekts ir virknes vai baitu objekts, kas attēlo ceļu.
režīms (pēc izvēles): Vesela skaitļa vērtība, kas apzīmē izveidojamā direktorija režīmu. Ja šis parametrs tiek izlaists, tiek izmantota noklusējuma vērtība Oo777.
dir_fd (pēc izvēles): Faila deskriptors, kas attiecas uz direktoriju. Šī parametra noklusējuma vērtība ir None.
Ja norādītais ceļš ir absolūts, dir_fd tiek ignorēts.
Piezīme: “*” parametru sarakstā norāda, ka visi turpmākie parametri (šeit mūsu gadījumā “dir_fd”) ir tikai atslēgvārdu parametri, un tos var nodrošināt, izmantojot to nosaukumu, nevis kā pozicionālo parametru.
Atgriešanas veids: Šī metode neatgriež nekādu vērtību.
1. piemērs: Izmantošanaos.mkdir()>direktorija/faila izveides metode
# Python program to explain os.mkdir() method> > # importing os module> import> os> > # Directory> directory>=> 'techcodeview.com'> > # Parent Directory path> parent_dir>=> 'D:/Pycharm projects/'> > # Path> path>=> os.path.join(parent_dir, directory)> > # Create the directory> # 'GeeksForGeeks' in> # '/home / User / Documents'> os.mkdir(path)> print>(>'Directory '% s' created'> %> directory)> > # Directory> directory>=> 'Geeks'> > # Parent Directory path> parent_dir>=> 'D:/Pycharm projects'> > # mode> mode>=> 0o666> > # Path> path>=> os.path.join(parent_dir, directory)> > # Create the directory> # 'GeeksForGeeks' in> # '/home / User / Documents'> # with mode 0o666> os.mkdir(path, mode)> print>(>'Directory '% s' created'> %> directory)> |
>
>
Izvade:
Directory 'techcodeview.com' created Directory 'Geeks' created>
2. piemērs: Kļūdas lietošanas laikāos.mkdir()>metodi.
definēt datoru
# Python program to explain os.mkdir() method> > # importing os module> import> os> > # Directory> directory>=> 'GeeksForGeeks'> > # Parent Directory path> parent_dir>=> 'D:/Pycharm projects/'> > # Path> path>=> os.path.join(parent_dir, directory)> > # Create the directory> # 'GeeksForGeeks' in> # '/home / User / Documents'> os.mkdir(path)> print>(>'Directory '% s' created'> %> directory)> > # if directory / file that> # is to be created already> # exists then 'FileExistsError'> # will be raised by os.mkdir() method> > # Similarly, if the specified path> # is invalid 'FileNotFoundError' Error> # will be raised> |
>
>
Izvade:
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'>
3. piemērs: Apstrādes kļūda lietošanas laikāos.mkdir()>metodi.
# Python program to explain os.mkdir() method> > # importing os module> import> os> > # path> path>=> 'D:/Pycharm projects / GeeksForGeeks'> > # Create the directory> # 'GeeksForGeeks' in> # '/home / User / Documents'> try>:> >os.mkdir(path)> except> OSError as error:> >print>(error)> |
>
>
Izvade:
[WinError 183] Cannot create a file when that file/ /already exists: 'D:/Pycharm projects/GeeksForGeeks'>
Izmantojot os.makedirs()
os.makedirs()>Python metode tiek izmantota, lai rekursīvi izveidotu direktoriju. Tas nozīmē, ka, veidojot lapu direktoriju, ja trūkst kāda vidēja līmeņa direktorija,os.makedirs()>metode radīs tos visus.
Piemēram, apsveriet šādu ceļu:
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil>
Pieņemsim, ka mēs vēlamies izveidot direktoriju “Nikhil”, bet direktorija “GeeksForGeeks” un “Autori” ceļā nav pieejami. Tados.makedirs()>metode radīs visus nepieejamos/trūkstošos direktorijus norādītajā ceļā. Vispirms tiks izveidots “GeeksForGeeks” un “Autori”, pēc tam tiks izveidots direktorijs “Nikhil”.
Sintakse: os.makedirs(ceļš, režīms = 0o777, pastāv_ok = False)
Parametrs:
ceļš: Ceļam līdzīgs objekts, kas attēlo failu sistēmas ceļu. Ceļam līdzīgs objekts ir virknes vai baitu objekts, kas attēlo ceļu.
režīms (pēc izvēles): Vesela skaitļa vērtība, kas apzīmē jaunizveidotā direktorija režīmu. Ja šis parametrs tiek izlaists, tiek izmantota noklusējuma vērtība Oo777.
exist_ok (pēc izvēles): Šim parametram tiek izmantota noklusējuma vērtība False. Ja mērķa direktorijs jau pastāv, tiek parādīts OSEror, ja tā vērtība ir False, pretējā gadījumā tā nav.Atgriešanas veids: Šī metode neatgriež nekādu vērtību.
1. piemērs: Izmantošanaos.makedirs()>direktorija izveides metode.
vai abstraktai klasei var būt konstruktors
# Python program to explain os.makedirs() method> > # importing os module> import> os> > # Leaf directory> directory>=> 'Nikhil'> > # Parent Directories> parent_dir>=> 'D:/Pycharm projects/GeeksForGeeks/Authors'> > # Path> path>=> os.path.join(parent_dir, directory)> > # Create the directory> # 'Nikhil'> os.makedirs(path)> print>(>'Directory '% s' created'> %> directory)> > # Directory 'GeeksForGeeks' and 'Authors' will> # be created too> # if it does not exists> > > > # Leaf directory> directory>=> 'c'> > # Parent Directories> parent_dir>=> 'D:/Pycharm projects/techcodeview.com/a/b'> > # mode> mode>=> 0o666> > path>=> os.path.join(parent_dir, directory)> > # Create the directory 'c'> > os.makedirs(path, mode)> print>(>'Directory '% s' created'> %> directory)> > > # 'GeeksForGeeks', 'a', and 'b'> # will also be created if> # it does not exists> > # If any of the intermediate level> # directory is missing> # os.makedirs() method will> # create them> > # os.makedirs() method can be> # used to create a directory tree> |
>
>
Izvade:
Directory 'Nikhil' created Directory 'c' created>
2. piemērs:
# Python program to explain os.makedirs() method> > # importing os module> import> os> > # os.makedirs() method will raise> # an OSError if the directory> # to be created already exists> > > # Directory> directory>=> 'Nikhil'> > # Parent Directory path> parent_dir>=> 'D:/Pycharm projects/GeeksForGeeks/Authors'> > # Path> path>=> os.path.join(parent_dir, directory)> > # Create the directory> # 'Nikhil'> os.makedirs(path)> print>(>'Directory '% s' created'> %> directory)> |
>
>
Izvade:
Traceback (most recent call last): File 'gfg.py', line 22, in os.makedirs(path) File 'C:UsersNikhil AggarwalAppDataLocalProgramsPython/ / Python38-32libos.py', line 221, in makedirs mkdir(name, mode) FileExistsError: [WinError 183] Cannot create a file when that/ / file already exists: 'D:/Pycharm projects/GeeksForGeeks/AuthorsNikhil'>
3. piemērs: Kļūdu apstrāde, izmantojot os.makedirs() metodi.
ja vēl paziņojumi java
# Python program to explain os.makedirs() method> > # importing os module> import> os> > # os.makedirs() method will raise> # an OSError if the directory> # to be created already exists> # But It can be suppressed by> # setting the value of a parameter> # exist_ok as True> > # Directory> directory>=> 'Nikhil'> > # Parent Directory path> parent_dir>=> 'D:/Pycharm projects/GeeksForGeeks/Authors'> > # Path> path>=> os.path.join(parent_dir, directory)> > # Create the directory> # 'Nikhil'> try>:> >os.makedirs(path, exist_ok>=> True>)> >print>(>'Directory '%s' created successfully'> %> directory)> except> OSError as error:> >print>(>'Directory '%s' can not be created'> %> directory)> > # By setting exist_ok as True> # error caused due already> # existing directory can be suppressed> # but other OSError may be raised> # due to other error like> # invalid path name> |
>
>
Izvade:
Directory 'Nikhil' created successfully>